赞
踩
我们讲的随机数其实暗指伪随机数。不少朋友可能想到C语言的rand(),可惜这个函数产生的随机数随机性非常差,而且速度很慢,相信几乎不能胜任一般的应用。
古老的LCG(linear congruential generator)代表了最好的伪随机数产生器算法。主要原因是容易理解,容易实现,而且速度快。这种算法数学上基于X(n+1) = (a * X(n) + c) % m这样的公式,其中:一般而言,高LCG的m是2的指数次幂(一般2^32或者2^64),因为这样取模操作截断最右的32或64位就可以了。多数编译器的库中使用了该理论实现其伪随机数发生器rand()。下面是部分编译器使用的各个参数值:
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 | 见Park–Miller随机数发生器 |
LCG不能用于随机数要求高的场合,例如不能用于Monte Carlo模拟,不能用于加密应用。
LCG有一些严重的缺陷,例如如果LCG用做N维空间的点坐标,这些点最多位于m1/n超平面上(Marsaglia定理),这是由于产生的相继X(n)值的关联所致。
另外一个问题就是如果m设置为2的指数,产生的低位序列周期远远小于整体。
一般而言,输出序列的基数b中最低n位,bk = m (k是某个整数),最大周期bn.
有些场合LCG有很好的应用,例如内存很紧张的嵌入式中,电子游戏控制台用的小整数,使用高位可以胜任。
LCG的一种C++实现版本如下:
- //************************************************************************
- // Copyright (C) 2008 - 2009 Chipset
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as
- // published by the Free Software Foundation, either version 3 of the
- // License, or (at your option) any later version.
- //
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //************************************************************************
- #ifndef LCRANDOM_HPP_
- #define LCRANDOM_HPP_
- #include <ctime>
-
- class lcrandom
- {
- public:
- explicit lcrandom(size_t s = 0) : seed(s)
- {
- if (0 == seed) seed = std::time(0);
- randomize();
- }
-
- void reset(size_t s)
- {
- seed = s;
- if (0 == seed) seed = std::time(0);
- randomize();
- }
-
- size_t rand()
- {
- //returns a random integer in the range [0, -1UL)
- randomize();
- return seed;
- }
-
- double real()
- {
- //returns a random real number in the range [0.0, 1.0)
- randomize();
- return (double)(seed) / -1UL;
- }
-
- private:
- size_t seed;
- void randomize() { seed = 1103515245UL * seed + 12345UL; }
- };
-
- class lcrand_help
- {
- static lcrandom r;
- public:
- lcrand_help() {}
- void operator()(size_t s) { r.reset(s); }
- size_t operator()() const { return r.rand(); }
- double operator()(double) { return r.real(); }
- };
- lcrandom lcrand_help:: r;
-
- extern void lcsrand(size_t s) { lcrand_help()(s); }
- extern size_t lcirand() { return lcrand_help()(); }
- extern double lcdrand() { return lcrand_help()(1.0); }
-
- #endif // LCRANDOM_HPP_

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。