How I select random numbers
Last update: Oct. 6, 2008
Some of your analysis is
based on random simulations. How did you select the random
numbers?
Until December 2001 I used
the stock random number generator that came with Microsoft
Visual C++ and Visual J++ to create programs. The rand()
function returns a "random" integer from 0 to
215-1 (32767). This function is flawed in that
the numbers are drawn in a cycle. After 231
(2,147,483,648) calls the cycle starts over again. I also
discovered that it repeats odd and even numbers in an even
smaller cycle of 217 (131,072).
From Dec 2001 to Oct 2008 I used what I’ll call the Nathan Reynolds code below. Nathan was very patient with me, helping me get this code to compile on Visual Studio 6.0, which wasn’t easy. It seemed to draw good random numbers, but was quite slow at only about 37,000 random numbers per second.
In 2008 someone wrote to me saying that the Nathan Reynolds code was much too slow, and suggested a Mersennse Twister random number generator. Despite trying for hours, I couldn’t get one to compile in Visual Studio 6.0. In October, 2008, I installed Visual Studio 2005, in hopes of getting a Mersenne Twister to work. Indeed, the code by Takuji Nishimura and Makoto Matsumoto compiled on the first try, after only adding the following line to the header:
using namespace std;
The speed, on the same computer, is about 5.7 million random numbers per second. I did a randomness test, taking the mod of 64 of 184 billion such random numbers. The result had a chi-squared statistic of 66.01523, with 63 degrees of freedom. The probability of results more skewed than that are 37.31%.
The code can be found on lots of web sites, including the one linked to above. If that URL goes dead, just do a search on the names above.
Nathan Reynolds Code
- Include the
following
#include
<windows.h>
#include <wincrypt.h>
- Add the following define
statement
#define _WIN32_WINNT
0x0400
- Add the following
function
int __fastcall RandNum()
{
static HCRYPTPROV Provider = NULL;
int RetValue;
if (!Provider)
{
if (!CryptAcquireContext(&Provider, NULL, NULL, PROV_RSA_FULL, 0))
if (!CryptAcquireContext(&Provider, NULL, NULL, PROV_RSA_FULL,
CRYPT_NEWKEYSET))
return(0);
RandNum(); // Throw out first number. Possibly non-random.
}
RetValue = 0;
if (!CryptGenRandom(Provider, sizeof(int), (unsigned char *) &RetValue))
RetValue = 0;
return(RetValue);
}
Calling RandNum returns a
random number from -232 to 232-1
(-2147483648 to 2147483647). On my slow 233 MgHZ computer it
can draw about 37000 per second. Over an eight hour test is
draw 1,063,000,000 numbers and it never drew the first
number twice. I also took the mod of 1,000,000 of each
number, multiplied by 1 if negative, and did a grouping
according the range. Following are the results.
|
Random Number Generator Test 1
|
|
Range
|
Observations
|
Expected
|
Chi-squared
|
|
0
|
1050
|
1063
|
0.16
|
|
1 to 9
|
9557
|
9567
|
0.01
|
|
10 to 99
|
95921
|
95670
|
0.66
|
|
100 to 999
|
955424
|
956700
|
1.7
|
|
1000 to 9999
|
9575467
|
9567000
|
7.49
|
|
10000 to 99999
|
95691752
|
95670000
|
4.95
|
|
100000 to 999999
|
956670829
|
956700000
|
0.89
|
|
Total
|
1063000000
|
1063000000
|
15.86
|
The probability of the
chi-squared statistic exceeding 15.86 with 6 degrees of
freedom is 0.014534477. In other words in a fair test the
results would be this skewed or more 1.45% of the
time.
Although I was happy the
number was on the chart I ran another test to see if it was
just low by chance or not. The next test took the mod of
100. Note that the mod of a negative number is also
negative, for example mod(-23,10)=-3. When the original
number is equally likely to be positive as negative then the
zero modulo will be double weighted, because -0=+0. The
following table displays the results of the second test,
which ran 9 hours and 1,184,000,000 hands.
|
Random Number Generator Test 2
|
|
Mod
|
Observations
|
Expected
|
Chi-squared
|
|
-99
|
5920346
|
5920000
|
0.020222
|
|
-98
|
5920494
|
5920000
|
0.041222
|
|
-97
|
5920108
|
5920000
|
0.00197
|
|
-96
|
5918596
|
5920000
|
0.332976
|
|
-95
|
5920750
|
5920000
|
0.095017
|
|
-94
|
5919747
|
5920000
|
0.010812
|
|
-93
|
5924155
|
5920000
|
2.91622
|
|
-92
|
5920946
|
5920000
|
0.151168
|
|
-91
|
5920494
|
5920000
|
0.041222
|
|
-90
|
5921236
|
5920000
|
0.258057
|
|
-89
|
5921122
|
5920000
|
0.212649
|
|
-88
|
5921583
|
5920000
|
0.423292
|
|
-87
|
5918048
|
5920000
|
0.643632
|
|
-86
|
5920845
|
5920000
|
0.120612
|
|
-85
|
5920411
|
5920000
|
0.028534
|
|
-84
|
5919251
|
5920000
|
0.094764
|
|
-83
|
5921314
|
5920000
|
0.291655
|
|
-82
|
5921162
|
5920000
|
0.228082
|
|
-81
|
5918218
|
5920000
|
0.536406
|
|
-80
|
5921196
|
5920000
|
0.241624
|
|
-79
|
5919536
|
5920000
|
0.036368
|
|
-78
|
5918839
|
5920000
|
0.227689
|
|
-77
|
5917620
|
5920000
|
0.956824
|
|
-76
|
5921994
|
5920000
|
0.671628
|
|
-75
|
5918671
|
5920000
|
0.298352
|
|
-74
|
5919746
|
5920000
|
0.010898
|
|
-73
|
5923185
|
5920000
|
1.713552
|
|
-72
|
5925156
|
5920000
|
4.490597
|
|
-71
|
5923517
|
5920000
|
2.089407
|
|
-70
|
5923458
|
5920000
|
2.019893
|
|
-69
|
5916754
|
5920000
|
1.779817
|
|
-68
|
5925094
|
5920000
|
4.383249
|
|
-67
|
5919818
|
5920000
|
0.005595
|
|
-66
|
5920407
|
5920000
|
0.027981
|
|
-65
|
5917265
|
5920000
|
1.263552
|
|
-64
|
5922194
|
5920000
|
0.813114
|
|
-63
|
5922081
|
5920000
|
0.731514
|
|
-62
|
5923239
|
5920000
|
1.772149
|
|
-61
|
5923228
|
5920000
|
1.760132
|
|
-60
|
5918294
|
5920000
|
0.491628
|
|
-59
|
5923562
|
5920000
|
2.143217
|
|
-58
|
5916623
|
5920000
|
1.926373
|
|
-57
|
5920450
|
5920000
|
0.034206
|
|
-56
|
5918690
|
5920000
|
0.289882
|
|
-55
|
5920430
|
5920000
|
0.031233
|
|
-54
|
5924020
|
5920000
|
2.729797
|
|
-53
|
5919749
|
5920000
|
0.010642
|
|
-52
|
5918481
|
5920000
|
0.389757
|
|
-51
|
5918437
|
5920000
|
0.412664
|
|
-50
|
5917577
|
5920000
|
0.991711
|
|
-49
|
5918087
|
5920000
|
0.61817
|
|
-48
|
5918648
|
5920000
|
0.308768
|
|
-47
|
5920751
|
5920000
|
0.09527
|
|
-46
|
5919160
|
5920000
|
0.119189
|
|
-45
|
5922085
|
5920000
|
0.734329
|
|
-44
|
5919853
|
5920000
|
0.00365
|
|
-43
|
5917061
|
5920000
|
1.459074
|
|
-42
|
5920388
|
5920000
|
0.02543
|
|
-41
|
5922511
|
5920000
|
1.065054
|
|
-40
|
5921283
|
5920000
|
0.278056
|
|
-39
|
5917098
|
5920000
|
1.422568
|
|
-38
|
5919836
|
5920000
|
0.004543
|
|
-37
|
5921625
|
5920000
|
0.446052
|
|
-36
|
5920441
|
5920000
|
0.032852
|
|
-35
|
5912388
|
5920000
|
9.787592
|
|
-34
|
5917239
|
5920000
|
1.287689
|
|
-33
|
5922804
|
5920000
|
1.328111
|
|
-32
|
5918245
|
5920000
|
0.520274
|
|
-31
|
5917850
|
5920000
|
0.780828
|
|
-30
|
5921663
|
5920000
|
0.467157
|
|
-29
|
5919177
|
5920000
|
0.114414
|
|
-28
|
5920925
|
5920000
|
0.144531
|
|
-27
|
5921598
|
5920000
|
0.431352
|
|
-26
|
5921787
|
5920000
|
0.53942
|
|
-25
|
5918688
|
5920000
|
0.290768
|
|
-24
|
5920267
|
5920000
|
0.012042
|
|
-23
|
5924130
|
5920000
|
2.881233
|
|
-22
|
5917813
|
5920000
|
0.807934
|
|
-21
|
5918894
|
5920000
|
0.206628
|
|
-20
|
5918101
|
5920000
|
0.609156
|
|
-19
|
5921174
|
5920000
|
0.232817
|
|
-18
|
5919903
|
5920000
|
0.001589
|
|
-17
|
5918201
|
5920000
|
0.546689
|
|
-16
|
5923168
|
5920000
|
1.695308
|
|
-15
|
5916071
|
5920000
|
2.607608
|
|
-14
|
5921278
|
5920000
|
0.275893
|
|
-13
|
5919447
|
5920000
|
0.051657
|
|
-12
|
5914697
|
5920000
|
4.750306
|
|
-11
|
5916899
|
5920000
|
1.624358
|
|
-10
|
5922077
|
5920000
|
0.728704
|
|
-9
|
5919833
|
5920000
|
0.004711
|
|
-8
|
5917148
|
5920000
|
1.37397
|
|
-7
|
5917180
|
5920000
|
1.343311
|
|
-6
|
5921722
|
5920000
|
0.500893
|
|
-5
|
5918886
|
5920000
|
0.209628
|
|
-4
|
5923230
|
5920000
|
1.762314
|
|
-3
|
5920555
|
5920000
|
0.052031
|
|
-2
|
5920900
|
5920000
|
0.136824
|
|
-1
|
5921649
|
5920000
|
0.459324
|
|
0
|
11841590
|
11840000
|
0.213522
|
|
1
|
5918149
|
5920000
|
0.57875
|
|
2
|
5915417
|
5920000
|
3.547954
|
|
3
|
5923245
|
5920000
|
1.77872
|
|
4
|
5918264
|
5920000
|
0.50907
|
|
5
|
5917667
|
5920000
|
0.919407
|
|
6
|
5919134
|
5920000
|
0.126682
|
|
7
|
5916007
|
5920000
|
2.693252
|
|
8
|
5919279
|
5920000
|
0.087811
|
|
9
|
5919787
|
5920000
|
0.007664
|
|
10
|
5917229
|
5920000
|
1.297034
|
|
11
|
5922223
|
5920000
|
0.834752
|
|
12
|
5917331
|
5920000
|
1.203304
|
|
13
|
5923263
|
5920000
|
1.798508
|
|
14
|
5919318
|
5920000
|
0.078568
|
|
15
|
5921365
|
5920000
|
0.314734
|
|
16
|
5920315
|
5920000
|
0.016761
|
|
17
|
5917176
|
5920000
|
1.347124
|
|
18
|
5919362
|
5920000
|
0.068757
|
|
19
|
5921946
|
5920000
|
0.639682
|
|
20
|
5921066
|
5920000
|
0.191952
|
|
21
|
5922382
|
5920000
|
0.958433
|
|
22
|
5917974
|
5920000
|
0.693357
|
|
23
|
5925734
|
5920000
|
5.553844
|
|
24
|
5923860
|
5920000
|
2.516824
|
|
25
|
5918473
|
5920000
|
0.393873
|
|
26
|
5918342
|
5920000
|
0.464352
|
|
27
|
5919210
|
5920000
|
0.105422
|
|
28
|
5920728
|
5920000
|
0.089524
|
|
29
|
5918298
|
5920000
|
0.489325
|
|
30
|
5919768
|
5920000
|
0.009092
|
|
31
|
5920684
|
5920000
|
0.07903
|
|
32
|
5923436
|
5920000
|
1.994273
|
|
33
|
5918742
|
5920000
|
0.267325
|
|
34
|
5920366
|
5920000
|
0.022628
|
|
35
|
5918574
|
5920000
|
0.343493
|
|
36
|
5919723
|
5920000
|
0.012961
|
|
37
|
5920658
|
5920000
|
0.073136
|
|
38
|
5920305
|
5920000
|
0.015714
|
|
39
|
5921699
|
5920000
|
0.487602
|
|
40
|
5919848
|
5920000
|
0.003903
|
|
41
|
5920014
|
5920000
|
0.000033
|
|
42
|
5919389
|
5920000
|
0.063061
|
|
43
|
5917141
|
5920000
|
1.380723
|
|
44
|
5918465
|
5920000
|
0.398011
|
|
45
|
5915247
|
5920000
|
3.816049
|
|
46
|
5917568
|
5920000
|
0.999092
|
|
47
|
5912907
|
5920000
|
8.49842
|
|
48
|
5923064
|
5920000
|
1.585827
|
|
49
|
5922388
|
5920000
|
0.963268
|
|
50
|
5917928
|
5920000
|
0.7252
|
|
51
|
5918286
|
5920000
|
0.496249
|
|
52
|
5919963
|
5920000
|
0.000231
|
|
53
|
5926660
|
5920000
|
7.4925
|
|
54
|
5921598
|
5920000
|
0.431352
|
|
55
|
5919325
|
5920000
|
0.076964
|
|
56
|
5920793
|
5920000
|
0.106224
|
|
57
|
5919584
|
5920000
|
0.029232
|
|
58
|
5924792
|
5920000
|
3.87893
|
|
59
|
5921625
|
5920000
|
0.446052
|
|
60
|
5920085
|
5920000
|
0.00122
|
|
61
|
5921000
|
5920000
|
0.168919
|
|
62
|
5922405
|
5920000
|
0.977031
|
|
63
|
5914832
|
5920000
|
4.511524
|
|
64
|
5918369
|
5920000
|
0.449352
|
|
65
|
5921440
|
5920000
|
0.35027
|
|
66
|
5923185
|
5920000
|
1.713552
|
|
67
|
5920475
|
5920000
|
0.038112
|
|
68
|
5919053
|
5920000
|
0.151488
|
|
69
|
5918946
|
5920000
|
0.187655
|
|
70
|
5921592
|
5920000
|
0.428119
|
|
71
|
5919229
|
5920000
|
0.100412
|
|
72
|
5919933
|
5920000
|
0.000758
|
|
73
|
5918332
|
5920000
|
0.46997
|
|
74
|
5918593
|
5920000
|
0.3344
|
|
75
|
5916092
|
5920000
|
2.579808
|
|
76
|
5918633
|
5920000
|
0.315657
|
|
77
|
5920171
|
5920000
|
0.004939
|
|
78
|
5920275
|
5920000
|
0.012774
|
|
79
|
5920520
|
5920000
|
0.045676
|
|
80
|
5920533
|
5920000
|
0.047988
|
|
81
|
5920574
|
5920000
|
0.055655
|
|
82
|
5919223
|
5920000
|
0.101981
|
|
83
|
5922429
|
5920000
|
0.996629
|
|
84
|
5919111
|
5920000
|
0.1335
|
|
85
|
5918512
|
5920000
|
0.374011
|
|
86
|
5918226
|
5920000
|
0.531601
|
|
87
|
5920132
|
5920000
|
0.002943
|
|
88
|
5916944
|
5920000
|
1.577557
|
|
89
|
5922948
|
5920000
|
1.468024
|
|
90
|
5920835
|
5920000
|
0.117774
|
|
91
|
5918626
|
5920000
|
0.318898
|
|
92
|
5920678
|
5920000
|
0.077649
|
|
93
|
5923583
|
5920000
|
2.168562
|
|
94
|
5920225
|
5920000
|
0.008552
|
|
95
|
5919980
|
5920000
|
0.000068
|
|
96
|
5920126
|
5920000
|
0.002682
|
|
97
|
5922269
|
5920000
|
0.869656
|
|
98
|
5915987
|
5920000
|
2.720299
|
|
99
|
5918674
|
5920000
|
0.297006
|
|
Total
|
1184000000
|
1184000000
|
175.301836
|
The probability of the
chi-squared statistic exceeding 175.3 with 198 degrees of
freedom is 0.875656. In other words in a totally random test
the results would be more skewed than this 87.57% of the
time.
In conclusion I feel this
method of random number generation is outstanding and I can
not prove any cycle or bias in it.
Note: Here is the URL
for HTML metacharacters:
www.netstrider.com/tutorials/HTMLRef/ASCII/metacharacters.html
.
©1998-2009 Wizard Of Odds Consulting, Inc. All rights reserved.
Privacy/Terms
Contact
Advertise
About Us
Links
|