- Home /
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?
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.
@sampenguin This is in fact still the case, the new Unity docs are just wrong.
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...
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!
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 ;)
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??
@kingdutka Yes, or alternatively Random.Range(0, 20) + 1
whichever reads the best in your eyes.
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
Yea I just used it for accessing an array after posting this and that use did occur to me.
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.
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.
Not terribly important, but a slight correction, the rand()
function in C only returns zero and positive integers.
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
A random float would just be like so:
int r = rand();
float fRange = max - $$anonymous$$;
return ((float)r / (float)$$anonymous$$AX_RAND) * fRange + $$anonymous$$;
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122955im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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