Two faults in "Adding Random Gameplay Elements"

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.

  1. 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.

  1. 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.

1 Like

Hi Hoxily,

Thanks for the pointers, yes I can see these errors. We’ll fix them asap, thanks!

The page has now been updated. Thanks again!
http://docs.unity3d.com/Manual/RandomNumbers.html