Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Klousianic · Apr 02, 2015 at 06:35 PM · javascriptinstantiaterandomrange

Problem with Random.Range

Hi everyone! I want to create an object, but just if the random.range(0,1) is 1. If it's 0, then nothing happens. Thank you and sorry for my bad english! Here is my code:

 var cube : GameObject;
 var spawnPosition : GameObject;
 var timer = 0.0;
 
 function SpawnCube() {
 
     var tempCubeSpawner = Instantiate(cube, spawnPosition.transform.position, Quaternion.identity);
 
 }
 
 function Update() {
 
     timer += Time.deltaTime;
     if(timer > 5) {
     var randomNumber = Random.Range(0,1);
     
     if(randomNumber == 0) {
       SpawnCube();
       timer = 0.0;
     }
       if(randomNumber == 1) {
       timer = 0.0;
     }
 
     }
 
 
 }
Comment
Add comment · Show 1
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 ozturkcompany · Apr 02, 2015 at 06:42 PM 0
Share

Why is your SpawnCube function above the function update? $$anonymous$$ake it below it

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by TheCatProblem · Apr 02, 2015 at 06:39 PM

The upper bound of Random.Range() is exclusive (i.e., it will never be selected). Thus Random.Range(0, 1) will always return 0. See the documentation for more details.

You could instead use var randomNumber = Random.Range(0.0f,1.0f) and check if randomNumber is less than 0.5f (which it should be roughly 50% of the time) instead of checking if it equals 0 or 1.

Comment
Add comment · Show 4 · 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 ozturkcompany · Apr 02, 2015 at 06:59 PM 1
Share

Correct! But it's not always excusive.

"Returns a random float number between and $$anonymous$$ [inclusive] and max [inclusive] (Read Only)."

"Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive] (Read Only)."

So $$anonymous$$lousianic, what you need to do is declaring your randomNumber variable as float and it will be all fine.

avatar image Klousianic · Apr 02, 2015 at 08:57 PM 0
Share

Thank you very much!

avatar image TheCatProblem · Apr 03, 2015 at 02:33 AM 0
Share

ozturkcompany, that's what I thought as well, but according to the documentation (at least, the version I linked to) the max value is exclusive for the float version, too. Of course, this could be (and probably is) just an error in the documentation.

avatar image Bunny83 · Apr 03, 2015 at 03:17 AM 1
Share

@TheCatProblem: Yep, could be a documentation error, but in the float case it doesn't really matter since the likelihood that 1.0 is returned is very low since it's a float value.

avatar image
3

Answer by DiegoSLTS · Apr 02, 2015 at 07:51 PM

Random.Range is overloaded, if you pass 2 int variables or values (explicit numbers WITHOUT a letter at the end) it'll return an integer random value between both values excluding the biggest one. If you pass at least one float variable or value (a number with an "f" at the end) it will return a real random value between both values, including the biggest one.

If you want your Random.Range to return 0 or 1 (and nothing in between) you should use Random.Range(0,2).

The suggestion of making the numbers float numbers and checking if they're greater than 0.5 is bad, it's a bad approach since it adds extra complexity to something that should be really simple, and if you for any change need a random value between 0, 1 and 2 in the future, you have to change that "is greater than 0.5" comparition since it doesn't make sense now.

Use the correct function for the result you want. You want random integer numbers, so use the Random.Range function that returns integer values.

Comment
Add comment · Show 1 · 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 TheCatProblem · Apr 03, 2015 at 02:53 AM 0
Share

In my opinion, use of the integer overload of Random.Range() should generally be limited to the selection of an item from a list, array, or other container of known size.

For a situation where one of N alternatives is to be selected at random, it's better practice to choose a random value in [0,1) and check if it lies in [(m-1)/N, m/N) for integer m in [1, N]. While marginally more complex, it's a better expression of the design intent (choose one of a number of equally probable outcomes).

In this case, it's only necessary to choose between two alternatives, so omitting the general logic and simply testing if the random value is less than 0.5f suffices.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with Random.range 1 Answer

Can't figure out next step for this code ? 1 Answer

Random.Range statement question 1 Answer

Instantiating a random dropped consumable item from many cloned objects 1 Answer

can your Random.Range parameters match with function perameter (javascript) 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