mscorlib(4.0.0.0) API with additions
Random.cs
2 
3 namespace System
4 {
7  [ComVisible(true)]
8  [__DynamicallyInvokable]
9  public class Random
10  {
11  private const int MBIG = int.MaxValue;
12 
13  private const int MSEED = 161803398;
14 
15  private const int MZ = 0;
16 
17  private int inext;
18 
19  private int inextp;
20 
21  private int[] SeedArray = new int[56];
22 
24  [__DynamicallyInvokable]
25  public Random()
26  : this(Environment.TickCount)
27  {
28  }
29 
32  [__DynamicallyInvokable]
33  public Random(int Seed)
34  {
35  int num = (Seed == int.MinValue) ? int.MaxValue : Math.Abs(Seed);
36  int num2 = 161803398 - num;
37  SeedArray[55] = num2;
38  int num3 = 1;
39  for (int i = 1; i < 55; i++)
40  {
41  int num4 = 21 * i % 55;
42  SeedArray[num4] = num3;
43  num3 = num2 - num3;
44  if (num3 < 0)
45  {
46  num3 += int.MaxValue;
47  }
48  num2 = SeedArray[num4];
49  }
50  for (int j = 1; j < 5; j++)
51  {
52  for (int k = 1; k < 56; k++)
53  {
54  SeedArray[k] -= SeedArray[1 + (k + 30) % 55];
55  if (SeedArray[k] < 0)
56  {
57  SeedArray[k] += int.MaxValue;
58  }
59  }
60  }
61  inext = 0;
62  inextp = 21;
63  Seed = 1;
64  }
65 
68  [__DynamicallyInvokable]
69  protected virtual double Sample()
70  {
71  return (double)InternalSample() * 4.6566128752457969E-10;
72  }
73 
74  private int InternalSample()
75  {
76  int num = inext;
77  int num2 = inextp;
78  if (++num >= 56)
79  {
80  num = 1;
81  }
82  if (++num2 >= 56)
83  {
84  num2 = 1;
85  }
86  int num3 = SeedArray[num] - SeedArray[num2];
87  if (num3 == int.MaxValue)
88  {
89  num3--;
90  }
91  if (num3 < 0)
92  {
93  num3 += int.MaxValue;
94  }
95  SeedArray[num] = num3;
96  inext = num;
97  inextp = num2;
98  return num3;
99  }
100 
103  [__DynamicallyInvokable]
104  public virtual int Next()
105  {
106  return InternalSample();
107  }
108 
109  private double GetSampleForLargeRange()
110  {
111  int num = InternalSample();
112  if ((InternalSample() % 2 == 0) ? true : false)
113  {
114  num = -num;
115  }
116  double num2 = num;
117  num2 += 2147483646.0;
118  return num2 / 4294967293.0;
119  }
120 
127  [__DynamicallyInvokable]
128  public virtual int Next(int minValue, int maxValue)
129  {
130  if (minValue > maxValue)
131  {
132  throw new ArgumentOutOfRangeException("minValue", Environment.GetResourceString("Argument_MinMaxValue", "minValue", "maxValue"));
133  }
134  long num = (long)maxValue - (long)minValue;
135  if (num <= int.MaxValue)
136  {
137  return (int)(Sample() * (double)num) + minValue;
138  }
139  return (int)((long)(GetSampleForLargeRange() * (double)num) + minValue);
140  }
141 
147  [__DynamicallyInvokable]
148  public virtual int Next(int maxValue)
149  {
150  if (maxValue < 0)
151  {
152  throw new ArgumentOutOfRangeException("maxValue", Environment.GetResourceString("ArgumentOutOfRange_MustBePositive", "maxValue"));
153  }
154  return (int)(Sample() * (double)maxValue);
155  }
156 
159  [__DynamicallyInvokable]
160  public virtual double NextDouble()
161  {
162  return Sample();
163  }
164 
169  [__DynamicallyInvokable]
170  public virtual void NextBytes(byte[] buffer)
171  {
172  if (buffer == null)
173  {
174  throw new ArgumentNullException("buffer");
175  }
176  for (int i = 0; i < buffer.Length; i++)
177  {
178  buffer[i] = (byte)(InternalSample() % 256);
179  }
180  }
181  }
182 }
Represents a pseudo-random number generator, which is a device that produces a sequence of numbers th...
Definition: Random.cs:9
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
virtual double Sample()
Returns a random floating-point number between 0.0 and 1.0.
Definition: Random.cs:69
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
virtual int Next(int minValue, int maxValue)
Returns a random integer that is within a specified range.
Definition: Random.cs:128
virtual int Next(int maxValue)
Returns a non-negative random integer that is less than the specified maximum.
Definition: Random.cs:148
virtual void NextBytes(byte[] buffer)
Fills the elements of a specified array of bytes with random numbers.
Definition: Random.cs:170
virtual int Next()
Returns a non-negative random integer.
Definition: Random.cs:104
virtual double NextDouble()
Returns a random floating-point number that is greater than or equal to 0.0, and less than 1....
Definition: Random.cs:160
static sbyte Abs(sbyte value)
Returns the absolute value of an 8-bit signed integer.
Definition: Math.cs:458
Random(int Seed)
Initializes a new instance of the T:System.Random class, using the specified seed value.
Definition: Random.cs:33
Random()
Initializes a new instance of the T:System.Random class, using a time-dependent default seed value.
Definition: Random.cs:25
Specifies that the class can be serialized.
Provides constants and static methods for trigonometric, logarithmic, and other common mathematical f...
Definition: Math.cs:10