Greetings,
According to unity documentation
chosenHair = Random.Range (1,3); should return the numbers 1, 2, or 3. For some reason mine is not returning 3, and when I highlight Random.Range I notice max is listed as “exclusive” which means it will not return 3. A quick solution would be to simply set the max as 4 instead, but I would rather know how to make the range all inclusive. Any ideas appreciated!
In short, a main reason why Random.Range() is Exclusive is for convenience of potentially needing one fewer defined variable. This can definitely apply to using the output value, but may very likely apply to the process of generating the value as well.
As an example use case, let’s consider getting a random value in an array:
public int arraySize = 10;
// ...
// First use: Creating the array
int[] array = new int[arraySize];
// Second use: Defining your iterator in a loop
for(int i = 0; i < arraySize; i++)
{
array _= (i * 50) - (i * i); // Example of populating the array_
}
// Third use: Getting a random value
int randomArrayValue = array[Random.Range(0, arraySize)];
By reusing arraySize in this example, simply changing its value will automatically propagate throughout its usage and, likewise, it doesn’t need any arbitrary adjustment (-1) just to be usable as the maximum in the Random.Range() call.