Search Unity

NextInt(min, max) slight incorrect?

Discussion in 'Entity Component System' started by diesoftgames, Jun 15, 2019.

  1. diesoftgames

    diesoftgames

    Joined:
    Nov 27, 2018
    Posts:
    122
    Code (CSharp):
    1. for (uint i = 1; i < 10000; i++) {
    2.     var random = new Unity.Mathematics.Random();
    3.     random.InitState(i);
    4.     UnityEngine.Debug.Log($"Two ranges: {random.NextInt(0, 1)}, {random.NextInt(0, 1)}");
    5. }
    This will just spit out "0, 0" everytime. If if make the max 2 instead of 1, I will start to see some 1s as well as 0s. It seems like the max is non-inclusive, which the parameter name does not suggest.
     
  2. ilih

    ilih

    Joined:
    Aug 6, 2013
    Posts:
    1,416
    Returns a uniformly random int value in the interval [min, max).
    i.e. min <= result < max, and with range 0..1, only 0 is valid result.
     
  3. diesoftgames

    diesoftgames

    Joined:
    Nov 27, 2018
    Posts:
    122
    While we're here:
    > Returns a uniformly random double value in the interval [0, 1).
    from https://docs.unity3d.com/Packages/c...ndom.html#Unity_Mathematics_Random_NextDouble
    Seems like it should be "[0, 1]" after doing some wikipedia reading on intervals to make sure I was clear on the terminology.

    Ultimately though I think the parameter name (the most visible documentation currently) of "max" is misleading, though it is handy because I don't have to subtract one from my array length.
     
  4. BrendonSmuts

    BrendonSmuts

    Joined:
    Jun 12, 2017
    Posts:
    86
    This is the exact same behavior the existing UnityEngine.Random class displays.
     
    charleshendry likes this.
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    And same behavior in standard C# Random class. (Next overload with min and max, non-inclusive max).
     
    charleshendry likes this.
  6. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    I have agree that is correct and always was done in that way but also agree that parameter name always was misleading and need to be renamed to something like maxExclusive so we can understand method logic from signature instead of from documentation.
     
    VirtusH, DragonCoder, Sarkahn and 3 others like this.