PeterO.RandomGenerator.UniformInt C# (CSharp) Method

UniformInt() public method

Generates a random 32-bit signed integer 0 or greater and less than the given number.
public UniformInt ( int maxExclusive ) : int
maxExclusive int One plus the largest possible value of /// the random number.
return int
        public int UniformInt(int maxExclusive)
        {
            if (maxExclusive < 0) {
              throw new ArgumentException("maxExclusive (" + maxExclusive +
            ") is less than 0");
            }
              if (maxExclusive <= 0) {
             return 0;
            }
              var b = new byte[4];
              switch (maxExclusive) {
            case 2: {
            this.valueIrg.GetBytes(b, 0, 1);
            return b[0] & 1;
              }
            case 256: {
            this.valueIrg.GetBytes(b, 0, 1);
            return (int)b[0] & 1;
              }
            default: {
            while (true) {
              var ib = 0;
              if (maxExclusive == 0x1000000) {
                this.valueIrg.GetBytes(b, 0, 3);
                ib = b[0] & 0xff;
                ib |= (b[1] & 0xff) << 8;
                ib |= (b[2] & 0xff) << 16;
                return ib;
              }
              if (maxExclusive == 0x10000) {
                this.valueIrg.GetBytes(b, 0, 2);
                ib = b[0] & 0xff;
                ib |= (b[1] & 0xff) << 8;
                return ib;
              }
              int maxexc;
              maxexc = (maxExclusive <= 100) ? 2147483600 :
                ((Int32.MaxValue / maxExclusive) * maxExclusive);
              while (true) {
                this.valueIrg.GetBytes(b, 0, 4);
                ib = b[0] & 0xff;
                ib |= (b[1] & 0xff) << 8;
                ib |= (b[2] & 0xff) << 16;
                ib |= (b[3] & 0x7f) << 24;
                if (ib < maxexc) {
                 return ib % maxExclusive;
                }
              }
            }
              }
              }
        }

Same methods

RandomGenerator::UniformInt ( int minInclusive, int maxExclusive ) : int

Usage Example

Beispiel #1
0
 public static String RandomDecimalString(RandomGenerator r)
 {
     int count = r.UniformInt(40) + 1;
       var sb = new StringBuilder();
       if (r.UniformInt(2) == 0) {
     sb.Append('-');
       }
       for (var i = 0; i < count; ++i) {
     if (i == 0 && count > 1) {
       sb.Append((char)('1' + r.UniformInt(9)));
     } else {
       sb.Append((char)('0' + r.UniformInt(10)));
     }
       }
       if (r.UniformInt(2) == 0) {
     sb.Append('.');
     count = r.UniformInt(30) + 1;
     for (var i = 0; i < count; ++i) {
       sb.Append((char)('0' + r.UniformInt(10)));
     }
       }
       if (r.UniformInt(2) == 0) {
     sb.Append('E');
      count = (r.UniformInt(100) < 10) ? r.UniformInt(5000) :
       r.UniformInt(20);
     if (count != 0) {
       sb.Append(r.UniformInt(2) == 0 ? '+' : '-');
     }
     sb.Append(TestCommon.IntToString(count));
       }
       return sb.ToString();
 }
All Usage Examples Of PeterO.RandomGenerator::UniformInt