我們講的隨機(jī)數(shù)其實(shí)暗指?jìng)坞S機(jī)數(shù)。提及隨機(jī)數(shù),不少朋友可能想到C語(yǔ)言的庫(kù)函數(shù)rand(),rand()隨機(jī)性太差,速度太慢。
古老的LCG(linear congruential generator)代表了最好的偽隨機(jī)數(shù)產(chǎn)生器算法。主要原因是容易理解,容易實(shí)現(xiàn),而且速度快。這種算法數(shù)學(xué)上基于X(n+1) = (a * X(n) + c) % m這樣的公式,其中:
模m, m > 0
系數(shù)a, 0 < a < m
增量c, 0 <= c < m
原始值(種子) 0 <= X(0) < m
其中參數(shù)c, m, a比較敏感,或者說(shuō)直接影響了偽隨機(jī)數(shù)產(chǎn)生的質(zhì)量。
一般而言,高LCG的m是2的指數(shù)次冪(一般2^32或者2^64),因?yàn)檫@樣取模操作截?cái)嘧钣业?2或64位就可以了。多數(shù)編譯器的庫(kù)中使用了該理論實(shí)現(xiàn)其偽隨機(jī)數(shù)發(fā)生器rand()。下面是部分編譯器使用的各個(gè)參數(shù)值:
Source m a c rand() / Random(L)的種子位
Numerical Recipes
2^32 1664525 1013904223
Borland C/C++
2^32 22695477 1 位30..16 in rand(), 30..0 in lrand()
glibc (used by GCC)
2^32 1103515245 12345 位30..0
ANSI C: Watcom, Digital Mars, CodeWarrior, IBM VisualAge C/C++
2^32 1103515245 12345 位30..16
Borland Delphi, Virtual Pascal
2^32 134775813 1 位63..32 of (seed * L)
Microsoft Visual/Quick C/C++
2^32 214013 2531011 位30..16
Apple CarbonLib
2^31-1 16807 0 見(jiàn)Park–Miller隨機(jī)數(shù)發(fā)生器
LCG不能用于隨機(jī)數(shù)要求高的場(chǎng)合,例如不能用于Monte Carlo模擬,不能用于加密應(yīng)用。
LCG有一些嚴(yán)重的缺陷,例如如果LCG用做N維空間的點(diǎn)坐標(biāo),這些點(diǎn)最多位于m1/n超平面上(Marsaglia定理),這是由于產(chǎn)生的相繼X(n)值的關(guān)聯(lián)所致。
另外一個(gè)問(wèn)題就是如果m設(shè)置為2的指數(shù),產(chǎn)生的低位序列周期遠(yuǎn)遠(yuǎn)小于整體。
一般而言,輸出序列的基數(shù)b中最低n位,bk = m (k是某個(gè)整數(shù)),最大周期bn.
有些場(chǎng)合LCG有很好的應(yīng)用,例如內(nèi)存很緊張的嵌入式中,電子游戲控制臺(tái)用的小整數(shù),使用高位可以勝任。
LCG的一種實(shí)現(xiàn)版本如下:
http://www.shnenglu.com/Chipset/archive/2008/12/20/69918.html如果需要高質(zhì)量的偽隨機(jī)數(shù),內(nèi)存充足(約2kb),Mersenne twister算法是個(gè)不錯(cuò)的選擇。Mersenne twister產(chǎn)生隨機(jī)數(shù)的質(zhì)量幾乎超過(guò)任何LCG。不過(guò)一般Mersenne twister的實(shí)現(xiàn)使用LCG產(chǎn)生種子。
Mersenne twister是Makoto Matsumoto (松本)和Takuji Nishimura (西村)于1997年開(kāi)發(fā)的偽隨機(jī)數(shù)產(chǎn)生器,基于有限二進(jìn)制字段上的矩陣線性再生。可以快速產(chǎn)生高質(zhì)量的偽隨機(jī)數(shù),修正了古老隨機(jī)數(shù)產(chǎn)生算法的很多缺陷。Mersenne twister這個(gè)名字來(lái)自周期長(zhǎng)度通常取Mersenne質(zhì)數(shù)這樣一個(gè)事實(shí)。常見(jiàn)的有兩個(gè)變種Mersenne Twister MT19937和Mersenne Twister MT19937-64。
Mersenne Twister有很多長(zhǎng)處,例如:周期2^19937 - 1對(duì)于一般的應(yīng)用來(lái)說(shuō),足夠大了,序列關(guān)聯(lián)比較小,能通過(guò)很多隨機(jī)性測(cè)試。
關(guān)于Mersenne Twister比較詳細(xì)的論述請(qǐng)參閱
http://www.shnenglu.com/Chipset/archive/2009/01/19/72330.html一種實(shí)現(xiàn)版本如下:
1 //************************************************************************
2 // This is a slightly modified version of Equamen mersenne twister.
3 //
4 // Copyright (C) 2009 Chipset
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Affero General Public License as
8 // published by the Free Software Foundation, either version 3 of the
9 // License, or (at your option) any later version.
10 //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Affero General Public License for more details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //************************************************************************
18
19 // Original Coyright (c) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura
20 //
21 // Functions for MT19937, with initialization improved 2002/2/10.
22 // Coded by Takuji Nishimura and Makoto Matsumoto.
23 // This is a faster version by taking Shawn Cokus's optimization,
24 // Matthe Bellew's simplification, Isaku Wada's real version.
25 // C++ version by Lyell Haynes (Equamen)
26 //
27 // Redistribution and use in source and binary forms, with or without
28 // modification, are permitted provided that the following conditions
29 // are met:
30 //
31 // 1. Redistributions of source code must retain the above copyright
32 // notice, this list of conditions and the following disclaimer.
33 //
34 // 2. Redistributions in binary form must reproduce the above copyright
35 // notice, this list of conditions and the following disclaimer in the
36 // documentation and/or other materials provided with the distribution.
37 //
38 // 3. The names of its contributors may not be used to endorse or promote
39 // products derived from this software without specific prior written
40 // permission.
41 //
42 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
46 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
48 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
49 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
50 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
51 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 //
54
55 #ifndef mtrandom_HPP_
56 #define mtrandom_HPP_
57
58 #include <stddef.h>
59
60 class mtrandom
61 {
62 public:
63 mtrandom() : left(1) { init(); }
64
65 explicit mtrandom(size_t seed) : left(1) { init(seed); }
66
67 mtrandom(size_t* init_key, int key_length) : left(1)
68 {
69 int i = 1, j = 0;
70 int k = N > key_length ? N : key_length;
71 init();
72 for(; k; --k)
73 {
74 state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1664525UL))+ init_key[j] + j; // non linear
75 state[i] &= 4294967295UL; // for WORDSIZE > 32 machines
76 ++i;
77 ++j;
78 if(i >= N)
79 {
80 state[0] = state[N - 1];
81 i = 1;
82 }
83 if(j >= key_length)
84 j = 0;
85 }
86
87 for(k = N - 1; k; --k)
88 {
89 state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1566083941UL)) - i; // non linear
90 state[i] &= 4294967295UL; // for WORDSIZE > 32 machines
91 ++i;
92 if(i >= N)
93 {
94 state[0] = state[N - 1];
95 i = 1;
96 }
97 }
98
99 state[0] = 2147483648UL; // MSB is 1; assuring non-zero initial array
100 }
101
102 void reset(size_t rs)
103 {
104 init(rs);
105 next_state();
106 }
107
108 size_t rand()
109 {
110 size_t y;
111 if(0 == --left)
112 next_state();
113 y = *next++;
114 // Tempering
115 y ^= (y >> 11);
116 y ^= (y << 7) & 0x9d2c5680UL;
117 y ^= (y << 15) & 0xefc60000UL;
118 y ^= (y >> 18);
119 return y;
120 }
121
122 double real() { return (double)rand() / -1UL; }
123
124 // generates a random number on [0,1) with 53-bit resolution
125 double res53()
126 {
127 size_t a = rand() >> 5, b = rand() >> 6;
128 return (a * 67108864.0 + b) / 9007199254740992.0;
129 }
130
131 private:
132 void init(size_t seed = 19650218UL)
133 {
134 state[0] = seed & 4294967295UL;
135 for(int j = 1; j < N; ++j)
136 {
137 state[j] = (1812433253UL * (state[j - 1] ^ (state[j - 1] >> 30)) + j);
138 // See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
139 // In the previous versions, MSBs of the seed affect
140 // only MSBs of the array state[].
141 // 2002/01/09 modified by Makoto Matsumoto
142 state[j] &= 4294967295UL; // for >32 bit machines
143 }
144 }
145
146 void next_state()
147 {
148 size_t* p = state;
149 int i;
150
151 for(i = N - M + 1; --i; ++p)
152 *p = (p[M] ^ twist(p[0], p[1]));
153
154 for(i = M; --i; ++p)
155 *p = (p[M - N] ^ twist(p[0], p[1]));
156 *p = p[M - N] ^ twist(p[0], state[0]);
157 left = N;
158 next = state;
159 }
160
161 size_t mixbits(size_t u, size_t v) const
162 {
163 return (u & 2147483648UL) | (v & 2147483647UL);
164 }
165
166 size_t twist(size_t u, size_t v) const
167 {
168 return ((mixbits(u, v) >> 1) ^ (v & 1UL ? 2567483615UL : 0UL));
169 }
170
171 static const int N = 624, M = 397;
172 size_t state[N];
173 size_t left;
174 size_t* next;
175 };
176
177 class mtrand_help
178 {
179 static mtrandom r;
180 public:
181 mtrand_help() {}
182 void operator()(size_t s) { r.reset(s); }
183 size_t operator()() const { return r.rand(); }
184 double operator()(double) { return r.real(); }
185 };
186 mtrandom mtrand_help:: r;
187
188 extern void mtsrand(size_t s) { mtrand_help()(s); }
189 extern size_t mtirand() { return mtrand_help()(); }
190 extern double mtdrand() { return mtrand_help()(1.0); }
191
192 #endif // mtrandom_HPP_
193