Search Unity

Bug Errors in Random.Range Documentation

Discussion in 'Documentation' started by WalterPalladino, Dec 18, 2022.

  1. WalterPalladino

    WalterPalladino

    Joined:
    Oct 24, 2016
    Posts:
    11
    At the Random.Range documentation https://docs.unity3d.com/ScriptReference/Random.Range.html
    Describing the second parameter first as inclusive and later as exclusive.


    You can read first:

    Declaration
    public static float Range(float minInclusive, float maxInclusive);
    Description
    Returns a random float within [minInclusive..maxInclusive] (range is inclusive).

    If minInclusive is greater than maxInclusive, then the numbers are automatically swapped.

    Important: Both the lower and upper bounds are inclusive. Any given float value between them, including both minInclusive and maxInclusive, will appear on average approximately once every ten million random samples.

    There is an int overload of this function that operates slightly differently, especially regarding the range maximum. See its docs below.

    See Random for details on the algorithm, and for examples of how UnityEngine.Random may be different from other random number generators.


    And later:

    Declaration
    public static int Range(int minInclusive, int maxExclusive);
    Description
    Return a random int within [minInclusive..maxExclusive) (Read Only).

    This method will behave in the following ways:

    • maxExcusive is exclusive, so for example Random.Range(0, 10) will return a value between 0 and 9, each with approximately equal probability.
    • If minInclusive and maxExclusive are equal, then the "exclusive rule" is ignored and minInclusive will be returned.
    • If minInclusive is greater than maxExclusive, then the numbers are automatically swapped.
    There is a float overload of this function that operates slightly differently, especially regarding the range maximum. See its docs above.

    See Random for details on the algorithm, and for examples of how UnityEngine.Random may be different from other random number generators.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    The integer version is intentionally exclusive on the upper end. This is so that you can pick numbers from a list's bounds or pixel boundaries more safely.

    For example,
    var card = UnityEngine.Random.Range(0, deck.Length);


    This would be safe with an upper-end exclusive limit, because you really want [ 0, deck.Length-1 ]. Way too many new coders would forget the -1 and get confusing OutOfBoundsExceptions thown, and to be honest, it's more natural to choose between [ 0, deck.Length ) instead of [ 0, deck.Length-1 ].

    It also mentions "other PRNGs."
    System.Random.NextDouble
    is a double float in the range of [ 0, 1 ), and will never return 1.0. This can easily be multiplied and converted to an integer, for the same benefit in accessing arrays, but can be confusing if it never returns the maximum. You're doing the right thing to always read the docs on what a PRNG returns.
     
  3. WalterPalladino

    WalterPalladino

    Joined:
    Oct 24, 2016
    Posts:
    11
    Thank you for your time describing this. It sounds reasonable but at the same time I think it is confuse.
    Will be great if more details like those ere added at the documentation page.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    I just don't know how much they can write. There's a hyperlink in the text you presented that says "Hey, this has some differences with other PRNGs, you should read about it." And then the page they provide gives all the same detail I just did.