Hey guys,
This is probably a bit of an inane question, but I’m curious.
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?
Siflou
3
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
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 
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.
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.