随机数产生的原理
在C语言中,随机数是通过“线性同余法”产生的。
seed = (seed*A + C) % M
通常A,C,M一般取质数。
如果定义了随机数函数如下:
void rand(int &seed)
{
seed = (seed*A+C)%M;
}
每次调用rand函数都会产生一个随机数给seed,实际上这是一个伪随机数,每当取相同的seed时,得到的序列相同。seed被称为种子,M应该尽可能大。
Random类的实现
// A very simple random number generator. Not especially good at
// generating truly random bits, but good enough for our needs in this
// package.
class Random {
private:
uint32_t seed_; //种子
public:
//0x7fffffffu = 2^23-1
//确保结果的范围在[0, 2^23-1)之间
explicit Random(uint32_t s) : seed_(s & 0x7fffffffu) { }
uint32_t Next() {
static const uint32_t M = 2147483647L; // 2^31-1
static const uint64_t A = 16807; // bits 14, 8, 7, 5, 2, 1, 0
// We are computing
// seed_ = (seed_ * A) % M, where M = 2^31-1
//
// seed_ must not be zero or M, or else all subsequent computed values
// will be zero or M respectively. For all other values, seed_ will end
// up cycling through every number in [1,M-1]
uint64_t product = seed_ * A;
// Compute (product % M) using the fact that ((x << 31) % M) == x.
seed_ = static_cast<uint32_t>((product >> 31) + (product & M));
// The first reduction may overflow by 1 bit, so we may need to
// repeat. mod == M is not possible; using > allows the faster
// sign-bit-based test.
if (seed_ > M) {
seed_ -= M;
}
return seed_;
}
// Returns a uniformly distributed value in the range [0..n-1]
// REQUIRES: n > 0
uint32_t Uniform(int n) { return Next() % n; }
// Randomly returns true ~"1/n" of the time, and false otherwise.
// REQUIRES: n > 0
bool OneIn(int n) { return (Next() % n) == 0; }
// Skewed: pick "base" uniformly from range [0,max_log] and then
// return "base" random bits. The effect is to pick a number in the
// range [0,2^max_log-1] with exponential bias towards smaller numbers.
uint32_t Skewed(int max_log) {
return Uniform(1 << Uniform(max_log + 1));
}
};
https://yq.aliyun.com/articles/2271
证明等式(product%M) == (product>>31)+(product&M)
成立。注:M等于2^31-1。
证明:
因为product类型是uint64_t,可以将product的二进制从左到右分解成高33位和低31位,假设高33位的值为H,低31位的值为L,
则product相当于高33位向左移动了31位加上低31位,即H<<31+L。
则product等于H*2^31+L
。由源码知道product=seed_*A
,而seed_和A都小于M,则H肯定小于M。
从而我们可以得到:
表达式左边product%M = (H*2^31+L)%M = (H*M+H+L)%M = H+L
。
表达式右边(product>>31) + (product&M) = (H*2^31 +L)>>31+L = (H*2^31+L)/2^31+L = H+L
。