Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by PeterDC · Sep 26, 2010 at 07:32 AM · javascriptrandomint

Random int Value

I'm making a game (aren't we all? ;) ) and I need a random value. I know how to do this with Random.Range, but for my game I need an int type, not float. Is there a way to round a float value to an int this in javascript with a rounding function or even just using the Random class? Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
23
Best Answer

Answer by Eric5h5 · Sep 26, 2010 at 07:34 AM

Random.Range returns an int if you use it with integers. Random.Range in the docs

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image · Sep 26, 2010 at 07:41 AM 0
Share

Doh, your answer hadn't appeared when I went to post. Deleting $$anonymous$$e.

avatar image PeterDC · Sep 26, 2010 at 07:45 AM 0
Share
@Eric5h5: Thanks for the quick answer, I apparently did not notice that when I looked up Random.Range in the the scripting manual...
avatar image NPSF3000 · Sep 26, 2010 at 08:28 AM 2
Share

Also look up $$anonymous$$athf - while you don't need it now it has a whole bunch of features including rounding that may be useful to you later on.

avatar image
36

Answer by khross · Mar 19, 2015 at 10:52 PM

Hi, its in the docs, but its important to mention the exclusive and inclusive difference. If using floats, the min and max value are inclusive, if using integers, the max value is exclusive.

Examples: When defining Random.Range(1.0f, 3.0f) we will get results from 1.0 to 3.0 When defining Random.Range(1,3) we will get results from 1 to 2

Regards

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by roymeredith · Jul 13, 2019 at 03:32 PM

you allways need to add one to he top number just the way unity works

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 13, 2019 at 07:34 PM 1
Share

That's not the way Unity works, that's how almost all random number generators for integer numbers work (processing, .NET / C#, Java, javascript, python, golang, Delphi, lisp, octave).


And you don't always have to add 1. One of the most common usage is this:

 SomeType[] someArray;
 
 someArray[Random.Range(0, someArray.Length)]


avatar image The_Pied_Shadow · Oct 11, 2021 at 06:57 PM 0
Share
as @Bunny83 says, this is not at all an "always". If it was "always" it wouldn't work this way. While it may not be the major use case @roymeredith has personally encountered, randomly selecting an integer is very often done to index to a random item in an array or other enumerated data collections. This will thus often be done with Random.random(0, someArray.Length) as @Bunny83 shows. If the max was inclusive in this case you'd have to very often SUBTRACT 1 to the length since indexing begins at 0. Since so much begins counting at 0 as opposed to 1 in coding, it just makes more sense that max be exclusive for integer randomization. Digressing a bit though my comment's purpose is not to just be redundant of @Bunny83 . There are still cases where you might want that max value to be inclusive. This is, like op said, solved by adding 1 to the max number. However, if you're doing this often this could end up looking un-elegant or unintuitive. So, if you're needing to do this often I'd suggest a wrapper for the random function that did this for you. Something like: public int InclusiveRandom(int $$anonymous$$, int max) { return Random.random($$anonymous$$, ++max); } I like to make a static Utility class which has little helper functions and wrappers like this for exactly this kind of thing.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Code to randomly generate a mesh? 0 Answers

Script not working (Java script) 2 Answers

Random spawn timer 1 Answer

GUI Text to GUI Label Script? 1 Answer

Mob Spawner, choosing a random mob 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges