Search Unity

Two faults in "Adding Random Gameplay Elements"

Discussion in 'Documentation' started by Hoxily, Jun 8, 2015.

  1. Hoxily

    Hoxily

    Joined:
    Jun 8, 2015
    Posts:
    1
    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.

    2. 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.
     
    Westland likes this.
  2. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358
    Hi Hoxily,

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

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358