PeterO.RandomGenerator.UniformLong C# (CSharp) Method

UniformLong() public method

Generates a random 64-bit signed integer within a given range.
public UniformLong ( long minInclusive, long maxExclusive ) : long
minInclusive long Smallest possible value of the random /// number.
maxExclusive long One plus the largest possible value of /// the random number.
return long
        public long UniformLong(long minInclusive, long maxExclusive)
        {
            if (minInclusive > maxExclusive) {
              throw new ArgumentException("minInclusive (" + minInclusive +
            ") is more than " + maxExclusive);
            }
            if (minInclusive == maxExclusive) {
             return minInclusive;
            }
              if (minInclusive >= 0) {
            return minInclusive + this.UniformLong(maxExclusive - minInclusive);
              } else {
              if ((maxExclusive < 0 && Int64.MaxValue + maxExclusive < minInclusive) ||
              (maxExclusive > 0 && Int64.MinValue + maxExclusive > minInclusive) ||
                minInclusive - maxExclusive < 0) {
               // Difference is greater than MaxValue
             long lb = 0;
             var b = new byte[8];
             while (true) {
               this.valueIrg.GetBytes(b, 0, 8);
               lb = b[0] & 0xffL;
               lb |= (b[1] & 0xffL) << 8;
               lb |= (b[2] & 0xffL) << 16;
               lb |= (b[3] & 0xffL) << 24;
               lb |= (b[4] & 0xffL) << 32;
               lb |= (b[5] & 0xffL) << 40;
               lb |= (b[6] & 0xffL) << 48;
               lb |= (b[7] & 0x7fL) << 56;
               if (lb >= minInclusive && lb < maxExclusive) {
             return lb;
               }
              }
            } else {
             return minInclusive + this.UniformLong(maxExclusive - minInclusive);
            }
              }
        }

Same methods

RandomGenerator::UniformLong ( long maxExclusive ) : long