blob: cbf2d21c4e5766543ada966420e6c27e68e93c78 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// random.h
//
//
// Defines a random number generator, initially from the System.Random code in the BCL. If you notice any problems,
// please compare to the implementation in src\mscorlib\src\system\random.cs.
//
// Main advantages over rand() are:
//
// 1) It generates better random numbers
// 2) It can have multiple instantiations with different seeds
// 3) It behaves the same regardless of whether we build with VC++ or GCC
//
// If you are working in the VM, we have a convenience method: code:GetRandomInt. This usess a thread-local
// Random instance if a Thread object is available, and otherwise falls back to a global instance
// with a spin-lock.
//
#ifndef _CLRRANDOM_H_
#define _CLRRANDOM_H_
#include <math.h>
//
// Forbid the use of srand()/rand(), as these are globally shared facilities and our use of them would
// interfere with native user code in the same process. This override is not compatible with stl headers.
//
#if !defined(DO_NOT_DISABLE_RAND) && !defined(USE_STL)
#ifdef srand
#undef srand
#endif
#define srand Do_not_use_srand
#ifdef rand
#undef rand
#endif
#define rand Do_not_use_rand
#endif //!DO_NOT_DISABLE_RAND && !USE_STL
class CLRRandom
{
private:
//
// Private Constants
//
static const int MBIG = INT_MAX;
static const int MSEED = 161803398;
static const int MZ = 0;
//
// Member Variables
//
int inext;
int inextp;
int SeedArray[56];
bool initialized;
public:
//
// Constructors
//
CLRRandom()
{
LIMITED_METHOD_CONTRACT;
initialized = false;
}
void Init()
{
LIMITED_METHOD_CONTRACT;
LARGE_INTEGER time;
if (!QueryPerformanceCounter(&time))
time.QuadPart = GetTickCount();
Init((int)time.u.LowPart ^ GetCurrentThreadId() ^ GetCurrentProcessId());
}
void Init(int Seed)
{
LIMITED_METHOD_CONTRACT;
int ii;
int mj, mk;
//Initialize our Seed array.
mj = MSEED - abs(Seed);
SeedArray[55]=mj;
mk=1;
for (int i=1; i<55; i++) { //Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position.
ii = (21*i)%55;
SeedArray[ii]=mk;
mk = mj - mk;
if (mk<0) mk+=MBIG;
mj=SeedArray[ii];
}
for (int k=1; k<5; k++) {
for (int i=1; i<56; i++) {
SeedArray[i] -= SeedArray[1+(i+30)%55];
if (SeedArray[i]<0) SeedArray[i]+=MBIG;
}
}
inext=0;
inextp = 21;
Seed = 1;
initialized = true;
}
bool IsInitialized()
{
LIMITED_METHOD_CONTRACT;
return initialized;
}
private:
//
// Package Private Methods
//
/*====================================Sample====================================
**Action: Return a new random number [0..1) and reSeed the Seed array.
**Returns: A double [0..1)
**Arguments: None
**Exceptions: None
==============================================================================*/
double Sample()
{
LIMITED_METHOD_CONTRACT;
//Including this division at the end gives us significantly improved
//random number distribution.
return (InternalSample()*(1.0/MBIG));
}
int InternalSample()
{
LIMITED_METHOD_CONTRACT;
int retVal;
int locINext = inext;
int locINextp = inextp;
if (++locINext >=56) locINext=1;
if (++locINextp>= 56) locINextp = 1;
retVal = SeedArray[locINext]-SeedArray[locINextp];
if (retVal == MBIG) retVal--;
if (retVal<0) retVal+=MBIG;
SeedArray[locINext]=retVal;
inext = locINext;
inextp = locINextp;
return retVal;
}
double GetSampleForLargeRange()
{
LIMITED_METHOD_CONTRACT;
// The distribution of double value returned by Sample
// is not distributed well enough for a large range.
// If we use Sample for a range [Int32.MinValue..Int32.MaxValue)
// We will end up getting even numbers only.
int result = InternalSample();
// Note we can't use addition here. The distribution will be bad if we do that.
bool negative = (InternalSample()%2 == 0) ? true : false; // decide the sign based on second sample
if( negative) {
result = -result;
}
double d = result;
d += (INT_MAX - 1); // get a number in range [0 .. 2 * Int32MaxValue - 1)
d /= 2*(unsigned int)INT_MAX - 1 ;
return d;
}
public:
//
// Public Instance Methods
//
/*=====================================Next=====================================
**Returns: An int [0..Int32.MaxValue)
**Arguments: None
**Exceptions: None.
==============================================================================*/
int Next()
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(initialized);
return InternalSample();
}
/*=====================================Next=====================================
**Returns: An int [minvalue..maxvalue)
**Arguments: minValue -- the least legal value for the Random number.
** maxValue -- One greater than the greatest legal return value.
**Exceptions: None.
==============================================================================*/
int Next(int minValue, int maxValue)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(initialized);
_ASSERTE(minValue < maxValue);
LONGLONG range = (LONGLONG)maxValue-minValue;
double result;
if( range <= (LONGLONG)INT_MAX)
result = (Sample() * range) + minValue;
else
result = (GetSampleForLargeRange() * range) + minValue;
_ASSERTE(result >= minValue && result < maxValue);
return (int)result;
}
/*=====================================Next=====================================
**Returns: An int [0..maxValue)
**Arguments: maxValue -- One more than the greatest legal return value.
**Exceptions: None.
==============================================================================*/
int Next(int maxValue)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(initialized);
double result = Sample()*maxValue;
_ASSERTE(result >= 0 && result < maxValue);
return (int)result;
}
/*=====================================Next=====================================
**Returns: A double [0..1)
**Arguments: None
**Exceptions: None
==============================================================================*/
double NextDouble()
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(initialized);
double result = Sample();
_ASSERTE(result >= 0 && result < 1);
return result;
}
/*==================================NextBytes===================================
**Action: Fills the byte array with random bytes [0..0x7f]. The entire array is filled.
**Returns:Void
**Arguments: buffer -- the array to be filled.
**Exceptions: None
==============================================================================*/
void NextBytes(__out_ecount(length) BYTE buffer[], int length)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(initialized);
for (int i=0; i<length; i++) {
buffer[i]=(BYTE)(InternalSample()%(256));
}
}
};
#endif //_CLRRANDOM_H_
|