leveldb源码学习--Random简单随机数生成

leveldbrandom.h中实现一个非常简单的随机数生成器,说成简陋也可以。

源码分析

构造函数

explicit Random(uint32_t s) : seed_(s & 0x7fffffffu) {
    // Avoid bad seeds.
    if (seed_ == 0 || seed_ == 2147483647L) {
      seed_ = 1;
    }
  }

构造函数用来初始化随机种子数,注意到作者认为当seed_0或者 M(2^31-1)时是一种不好的种子数。这是因为在后面的随机数生成方法中采用的是seed_ = (seed_ * A) % M,如果seed_0或者M,那么以后产生的所有seed_都是0了(具体表现在Next()函数中)。

Next()

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_;
  }

Next()函数中,开始我没看明白的一点是seed_ 如何由product得来的,其实注意到product是一个uint64_t,而seed_Muint32_t。如同注释中提示到的Compute (product % M) using the fact that ((x << 31) % M) == x.,将(product % M)分成高32位的结果和低32位的结果,然后相加即可。(不知道为何能分开求的可以去面壁了)

Uniform() && OneIn()

  // 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()

// 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));
  }

这个函数随机返回[0, 2^(max_log-1)]区间中的一个2的指数值。

ice_city liuhiter@gmail.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 方法1 (数据类型)(最小值+Math.random()*(最大值-最小值+1)) 例: (int)(1+Math...
    GB_speak阅读 41,327评论 2 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,421评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,728评论 0 17
  • 1
    Julie_134a阅读 1,001评论 0 0
  • 使用和研究过这么多程序语言之后,我觉得几乎不包含多余功能的语言,只有一个:Scheme。所以我觉得它是学习程序设计...
    daydreamly阅读 7,517评论 1 13