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
0
Question by Goldseeker · Apr 09, 2015 at 02:16 PM · unity 5randomrangedocumentation

Random.Range changed in Unity 5?

In online documentation it states currently that Random.Range(float a, float b) will return value in range [a, b), but in earlier versions of documentation it was [a, b], so i wonder if they've changed function behavior(which is crazy it would lead to impossible to debug miracle bugs) or it is a simple documentation mistake?

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 Fattie · May 16, 2015 at 06:14 AM 0
Share

watch out for the difference in int versus float versions??

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Owen-Reynolds · Apr 09, 2015 at 02:40 PM

Even if it is a change, I can't see it breaking any real code. And it looks like just a doc fix.

In theory [1.0,2.0] could generate 2.0 exactly, but no one would ever count on actually rolling a 2.00000. The odds are 1 out of 2^23(?). And losing that number changes the probability space by an infinitesimal amount.

And everyone "knows" all random funcs never rolls the top number. I suspect most people, myself included, just assumed the range was really the standard [) or maybe even (). The normal way to write code with random float ranges is to assume it will never be an end value, but allow that it could be.

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 Goldseeker · Apr 09, 2015 at 03:04 PM 0
Share

So if it infinitesimal (with this i'm not quite agree) why change it at all? [) is by no means standart (c++ rand() is [0, RAND_$$anonymous$$AX] for example)

avatar image Bonfire-Boy · Apr 09, 2015 at 04:52 PM 0
Share

But what Owen says about how to code using random floats applies just as much in C++ as in Unity. The point is that getting a random float and then testing its equality to any specific value (not just the bounds) is a weird thing to do.

avatar image
1

Answer by Bunny83 · May 16, 2015 at 05:45 AM

I can verify that it seems they just messed up the documentation (once again^^). Here's a test that proofs that the max value is inclusive:

 IEnumerator Start()
 {
     float max = 2.0f;
     while (!Input.GetKeyDown(KeyCode.Return))
     {
         for (int i = 0; i < 100000; i++)
         {
             float v = Random.Range(0f, max);
             if (v == max)
                 Debug.Log("Match " + v.ToString("0.000000000"));
         }
         yield return null;
     }
 }

At about 75 fps (7.5 million values per sec.) I get a "Match" on average every 2 - 4 seconds.

So nothing has changed, just the docs. Feel free to file a bug report in Unity. There's a category for documentation.

edit
btw. "RAND_MAX" in C++ isn't a variable you set. It's just a constant and it represents by definition "the maximum value returned by rand". Most random generators produce values within the32 or 64 bit unsigned integer range. The way you apply a max value is by using the modulo on the returned number. The modulo automatically excludes this value:

 0 % 3 == 0
 1 % 3 == 1
 2 % 3 == 2
 3 % 3 == 0
 4 % 3 == 1
 5 % 3 == 2
 6 % 3 == 0

It's the same logic as with zero based indices. Since zero is a value as well the value you specify as max devines the count of possible values.

for mix max you usually use:

 min + rand() % (max-min)

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 Fattie · May 16, 2015 at 06:01 AM 0
Share

spectacular research!!!!

avatar image Eric5h5 · May 16, 2015 at 06:13 AM 0
Share

But the docs say "Returns a random float number between and $$anonymous$$ [inclusive] and max [inclusive] (Read Only)." Which is what the Unity 4 docs say.

avatar image Bunny83 · May 16, 2015 at 06:37 AM 0
Share

Yes, it used to be both inclusive for the float version. However they have changed it in the docs.

The docs are changed way to often and the funny thing is they always change things which don't need to be changed and those things which are wrong for ages stay ^^.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how to have object fall using random speed 0 Answers

Avoid getting same object as previous one with Random.Range? 2 Answers

Moving a object randomly without it being inside a wall 1 Answer

Excluding values from a Random.range? 1 Answer

Random.Range statement question 1 Answer


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