I recently updated Unity to version 5.4.1 from 5.3.1, and I noticed that in one of my animation routines, one animation clip was no longer being played. Upon inspection, I discovered that one array element out of an array of four animation clips was not being played any longer (specifically, the last element). The script that I have attached uses the Range.Random function (integer version), to randomise which animation clip is played.
v = Random.Range (0, 3)
animator.SetInteger ("AnimIndex", v);
which I have since adjusted to:
v = Random.Range (0, 4)
animator.SetInteger ("AnimIndex", v);
The animations are now working (completely) again. Note that I have not added any new animations in since the update.
Question: Has the integer version of the Random.Range function been changed to have an exclusive max index, rather than an inclusive one? If so, was this to provide consistency over both integer and float versions of the function? Secondly, why was it not mentioned in the release notes for the latest update, or updated in the documentation?
I don’t think this was changed (I am using 5.3.5 and it is exclusive there as well) and the documentation clearly states that the max value of the int version is exclusive:
Int version has always had an open right end. I personally don’t find it inconsistent. Int and float are conceptually different. But you are right, not very intuitive.
What you’re specifying is a range of integers among which you want a random pick with a uniform probability distribution. Imagine you’d specify a range [1…3]. Although you kind of give 3 points on the number line you actually only split this segment into 2 sub-segments. And a randomly chosen point on that whole segment is being floored down to its left end. So everything between 1 and 2 becomes 1 and everything between 2 and 3 becomes 2. This is of course highly arguable.
In your case Random.Range(0, 3) will produce 0, 1, 2.
It’s also conveniet in conjunction with lists/arrays, you don’t have to do -1:
elements[Random.Range(0, elements.Count)]
PS: I myself got confused with this many times. I think most frequently what happens is people forget to write 0.0F instead of 0 and the other function is used implicitly.