LevelDB源码分析7-随机数产生

本文介绍了C语言中随机数的生成原理,采用线性同余法,详细解析了一个简单的随机数生成器类Random的实现过程,并证明了关键数学运算等式的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

随机数产生的原理

在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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值