GitHub: https://github.com/storagezhang

Emai: debugzhang@163.com

华为云社区: https://bbs.huaweicloud.com/blogs/249894

LevelDB: https://github.com/google/leveldb

C 语言中伪随机数生成算法实际上是采用了"线性同余法":

\(seed = (seed * A + C ) \% M\)

其中 \(A,C,M\) 都是常数(一般会取质数)。当 \(C=0\) 时,叫做乘同余法。

假设定义随机数函数

void rand(int &seed)
{
seed = (seed * A + C ) % M;
}

每次调用 rand 函数都会产生一个随机值赋值给 seed,实际上 rand 函数生成的随机数是一个递推序列,初值为 seed。所以当初始的 seed 相同时,得到的递推序列也会相同。我们称 seed 为随机数种子,称 rand 生成的随机数为伪随机数,一个伪随机数常用的原则就是 M 尽可能的大。

在 LevelDB 的随机数类 Random 类中,\(A=16807, M=2147483647, C=0\):

explicit Random(uint32_t s) : seed_(s & 0x7fffffffu) {
// Avoid bad seeds.
if (seed_ == 0 || seed_ == 2147483647L) {
seed_ = 1;
}
} 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_;
}

源码中利用 (product >> 31) + (product & M) 来代替 product % M,主要是为了避免 64 位除法。

下面证明 \(product\ \%\ M = (product >> 31) + (product\ \&\ M)\):

\[\begin{align}

&将\ product\ 分为高\ 33\ 位和低\ 31\ 位 \\
\\
&令高\ 33\ 位的值为\ H,低\ 31\ 位的值为\ L \\
\\
&则\ product = H << 31 + L = H \cdot 2^{31}+L = H \cdot M + L \\
\\
&因为\ product = seed \cdot A, 且\ seed\ 和\ A\ 都小于\ M,故\ H\ 必小于\ M \\
\\
&等式左边 = product \%\ M = (H \cdot M+L) \%\ M = (H + L) \%\ M \\
\\
&等式右边 = (product >> 31) + (product\ \&\ M) = (H \cdot 2^{31}+L)>>31 + L = H + L \\
\end{align}
\]

此时考虑下方的 if 语句:

if (seed_ > M) {
seed_ -= M;
}

由于 \(H\) 和 \(L\) 都小于 \(M\),故 \(H+M<2L\)。

经过语句,等式右边也等于 \((H + L) \%\ M\) 了。

综上,等式成立

LevelDB 源码解析之 Random 随机数的更多相关文章

  1. Leveldb源码解析之Bloom Filter

    Bloom Filter,即布隆过滤器,是一种空间效率很高的随机数据结构. 原理:开辟m个bit位数组的空间,并全部置零,使用k个哈希函数将元素映射到数组中,相应位置1.如下图,元素K通过哈希函数h1 ...

  2. LevelDB 源码解析之 Arena

    GitHub: https://github.com/storagezhang Emai: debugzhang@163.com 华为云社区: https://bbs.huaweicloud.com/ ...

  3. LevelDB 源码解析之 Varint 编码

    GitHub: https://github.com/storagezhang Emai: debugzhang@163.com 华为云社区: https://bbs.huaweicloud.com/ ...

  4. jQuery2.x源码解析(缓存篇)

    jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 缓存是jQuery中的又一核心设计,jQuery ...

  5. 【JUC源码解析】Exchanger

    简介 Exchanger,并发工具类,用于线程间的数据交换. 使用 两个线程,两个缓冲区,一个线程往一个缓冲区里面填数据,另一个线程从另一个缓冲区里面取数据.当填数据的线程将缓冲区填满时,或者取数据的 ...

  6. 谷歌BERT预训练源码解析(一):训练数据生成

    目录预训练源码结构简介输入输出源码解析参数主函数创建训练实例下一句预测&实例生成随机遮蔽输出结果一览预训练源码结构简介关于BERT,简单来说,它是一个基于Transformer架构,结合遮蔽词 ...

  7. Theano:LSTM源码解析

    最难读的Theano代码 这份LSTM代码的作者,感觉和前面Tutorial代码作者不是同一个人.对于Theano.Python的手法使用得非常娴熟. 尤其是在两重并行设计上: ①LSTM各个门之间并 ...

  8. Java 集合系列13之 WeakHashMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对WeakHashMap进行学习.我们先对WeakHashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用WeakHashMap.第1部分 WeakHashMap介绍 ...

  9. Java 集合系列16之 HashSet详细介绍(源码解析)和使用示例

    概要 这一章,我们对HashSet进行学习.我们先对HashSet有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashSet.内容包括:第1部分 HashSet介绍第2部分 HashSe ...

随机推荐

  1. How To Install Linux & Nginx & MySQL & PHP (LEMP) stack on Raspberry Pi 3,Raspberry Pi 3,LEMP,Nginx,PHP, LEMP (not LNMP)

    1.   How To Install Linux & Nginx & MySQL & PHP (LEMP) stack on Raspberry Pi 3         R ...

  2. macOS & pbcopy

    macOS & pbcopy copy from command line pbcopy $ whoami | pbcopy # xgqfrms-mbp $ echo "hello, ...

  3. Next.js 10

    Next.js 10 October 27th 2020 https://nextjs.org/blog/next-10 refs xgqfrms 2012-2020 www.cnblogs.com ...

  4. 算法的时间复杂度 & 性能对比

    算法的时间复杂度 & 性能对比 累加算法性能对比 // js 累加算法性能对比测试 const n = 10**6; (() => { console.time(`for`); let ...

  5. Python Learning Paths

    Python Learning Paths Python Expert Python in Action Syntax Python objects Scalar types Operators St ...

  6. Android Studio show whitespace & Android studio 设置注释缩进

    Android Studio show whitespace & Android studio 设置注释缩进 https://github.com/xgqfrms/flutter/issues ...

  7. outlook & email & animation

    outlook & email & animation tada position div

  8. js 斩掉单行注释和多行注释

    var json = ` // e { /* hello */ name:/* a */ 'ajanuw' // c /** * * hello * ? adsd * todo */ // c } ` ...

  9. html2Canvas to Images

    <script> $(function () { var content = document.getElementById("shareImages"); conte ...

  10. 「NGK每日快讯」11.23日NGK公链第21期官方快讯!