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
3
Question by The_Magical_Kiwi · Nov 28, 2013 at 01:47 AM · c#random.range

Why is Random.Range exclusive for integers but inclusive for floats?

Hey guys,

This is probably a bit of an inane question, but I'm curious.

http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

Why are the arguments for the float one [inclusive], [inclusive] and for the integer one [inclusive], [exclusive]. Is there some technical reason for the discrepancy or is it just arbitrary?

Comment
Add comment · Show 5
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 sampenguin · May 20, 2015 at 07:33 PM 0
Share

Actually as of Unity 5, this is no longer the case. Both versions (int and float) are documented as maximally exclusive. Haven't tested to verify but that's a subtle change that can cause some interesting statistical issues for existing code.

avatar image numberkruncher · May 21, 2015 at 09:00 AM 0
Share

@sampenguin This is in fact still the case, the new Unity docs are just wrong.

avatar image sampenguin · May 21, 2015 at 04:37 PM 0
Share

ha! Unity docs being wrong? Impossible! :P

avatar image cjdev · May 21, 2015 at 06:43 PM 0
Share

If you take Random.Range(Single.$$anonymous$$axValue - 1f, Single.$$anonymous$$axValue) you will occasionally get Single.$$anonymous$$axValue. To me that means it's inclusive...

avatar image sampenguin · May 21, 2015 at 08:44 PM 0
Share

Yep I believe it. I was being sarcastic, Unity docs are of course far from perfect. When in doubt, see what it does in reality. Thx for verifying the Unity 5 docs are in error, and please disregard my initial comment!

4 Replies

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

Answer by numberkruncher · Nov 28, 2013 at 02:18 AM

Though this may not be the case with Unity's implementation of their random functions, based upon my previous experience this is due to the way in which the random numbers are generated.

Random Integers

Random integers can be generated between 0 and the largest representable integer int.MaxValue. Now you want to get a random value between a specific minimum and maximum value so the following formula can be used:

 public static int RandomInteger(int minInclusive, int maxExclusive) {
     int randomInteger = SomeRandomNumberGeneratorAlgorithm();
     int range = maxExclusive - minInclusive;
     return minInclusive + randomInteger % range;
 }

If randomInteger can be any positive integer then the above will produce values >= minInclusive and < maxExclusive.

Random Floats

Random floats can be generated using a function like the followings:

 public static float RandomFloat(float minInclusive, float maxInclusive) {
     int randomInteger = SomeRandomNumberGeneratorAlgorithm();
     // Convert to a value between 0f and 1f:
     float randomFloat = (float)randomInteger / (float)int.MaxValue;

     float range = maxInclusive - minInclusive;
     return minInclusive + randomFloat * range;
 }

Random values generated with this function are inclusive of both the user specified minimum and maximum values.

Warning: Above snippets have not been tested, but I am pretty sure I got those right ;)

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 kingdutka · Apr 01, 2014 at 05:32 PM 0
Share

Just to be clear, if I were making a dice roller using integers and I wanted to roll on a 20-sided die, I would need:

int roll = Random.Range(1,21);

NOT

int roll = Random.Range(1,20);

Correct??

avatar image numberkruncher · Apr 02, 2014 at 11:36 AM 0
Share

@kingdutka Yes, or alternatively Random.Range(0, 20) + 1 whichever reads the best in your eyes.

avatar image
6

Answer by Siflou · Nov 28, 2013 at 02:21 AM

Hey There,

I think it is just for convinience. For example, if you have an array of 10 objects and that you want to select a random object in that array, you could simply do myArray[Random.Range(0,myArray.Length)]; and you wouldn't have to worry with the -1 because the indexes in the array are [0,9]. For the float, take for example if I want to set an animation to a random time between 0 and 1, I could just do myAnim["walk"].time = Random.Range(0f,1f); so it would include all of the animation's frame. So I really think that it is just for convinience. Hope it helps :). Have a great day.

Claude

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 The_Magical_Kiwi · Nov 28, 2013 at 02:36 AM 0
Share

Yea I just used it for accessing an array after posting this and that use did occur to me.

avatar image numberkruncher · Nov 28, 2013 at 02:41 AM 0
Share

I think that this is for convenience, but implementation-wise rather than usage since an inclusive range would require a few more instructions which require additional user code to become exclusive again. Whilst the performance difference would be tiny, people should only pay for what they use. That would be my reason for implementing it this way.

avatar image
1

Answer by SilentSin · Nov 28, 2013 at 02:29 AM

Note: none of this will make sense unless you understand how modulus works (feel free to look it up, its not particularly complicated).

In c++ to get a random value, you call rand() which returns any int value. To get values in a range like unity gives you would have something like:

 public static int Range(int min, int max)
 {
     int randomValue = rand();
     int range = max - min;// The possible difference in values between min and max.
     return min + randomValue % range;
 }

So for example calling Range(0, 10) would have

 range = 10 - 0

resulting in:

 return 0 + randomValue % 10;

When you modulus any number by 10, you'll get a number from 0 to 9, aka Range() can be said to be [inclusive], [exclusive].

On the other hand, I have no clue how you would get a random float value but i suspect that the difference is what causes it to be [inclusive], [inclusive]. So yes, I believe there is probably a technical reason but I'm not sure exactly what it is.

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 numberkruncher · Nov 28, 2013 at 02:37 AM 0
Share

Not terribly important, but a slight correction, the rand() function in C only returns zero and positive integers.

avatar image The_Magical_Kiwi · Nov 28, 2013 at 02:40 AM 1
Share

Interesting, I can see where you are co$$anonymous$$g from.

$$anonymous$$y ego requires me to point out that I already knew about modulus :P sigh of relief

avatar image simmania · Apr 13, 2015 at 12:07 PM 0
Share

A random float would just be like so:

 int r = rand();
 float fRange = max - $$anonymous$$;
 return ((float)r / (float)$$anonymous$$AX_RAND) * fRange + $$anonymous$$;
avatar image
0

Answer by DarthHawk13 · Feb 15, 2017 at 06:16 AM

Actually to use the int values for Random.Range just add 1 to the array like so

 GameObject newEnemy = Instantiate (Enemies [Random.Range(0,3)]) as GameObject;

It works in my code when the array only has 3 elements not 4. I tried the 'Random.Range(0,2)+1' and it didn't work for me but this code does and has a chance to spawn all three.

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

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

random not working 2 Answers

Semi-Random Or Engine 1 Answer

Random Numbers Always The Same? 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