There are two faults in unity manual page at http://docs.unity3d.com/Manual/RandomNumbers.html .
I’ll show the comparision of the original part to the corrected part. The differences are emphasized by bold and red color.
- The first fault – omit the parenthesis
---- original ----
// Adding 0.0 is simply to cast the integers to float for the division.
var prob = numToChoose + 0.0 / numLeft + 0.0;
---- corrected ----
// Adding 0.0 is simply to cast the integers to float for the division.
var prob = (numToChoose + 0.0) / (numLeft + 0.0);
It need parenthesis so that addition operations are computed first.
- The second fault – omit explicit float cast
---- original ----
int numToChoose = numRequired;
for (int numLeft = spawnPoints.Length; numLeft > 0; numLeft–) {
float prob = numToChoose/numLeft;
---- corrected ----
int numToChoose = numRequired;
for (int numLeft = spawnPoints.Length; numLeft > 0; numLeft–) {
float prob = **(float)numToChoose/(float)**numLeft;
Use explicit type cast to cast the integers to float for the float-division, otherwise “/” will be integer-division.