2 * Eddie Kohler, Yandong Mao, Robert Morris
3 * Copyright (c) 2012 President and Fellows of Harvard College
4 * Copyright (c) 2012 Massachusetts Institute of Technology
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, subject to the conditions
9 * listed in the Masstree LICENSE file. These conditions include: you must
10 * preserve this copyright notice, and you cannot mention the copyright
11 * holders in advertising related to the Software without their permission.
12 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13 * notice is a summary of the Masstree LICENSE file; the license in that file is
21 // A simple LCG with parameters from Numerical Recipes.
22 class kvrandom_lcg_nr_simple { public:
23 enum { min_value = 0, max_value = 0xFFFFFFFFU };
24 typedef uint32_t value_type;
25 typedef uint32_t seed_type;
26 kvrandom_lcg_nr_simple()
27 : seed_(default_seed) {
29 explicit kvrandom_lcg_nr_simple(seed_type seed)
32 void reset(seed_type seed) {
36 return (seed_ = seed_ * a + c);
40 enum { default_seed = 819234718U, a = 1664525U, c = 1013904223U };
43 // A combination version of the NR LCG that uses only its higher order
44 // digits. (In the default NR LCG the lowest bits have less randomness; e.g.,
45 // the low bit flips between 0 and 1 with every call.)
46 class kvrandom_lcg_nr : public kvrandom_lcg_nr_simple { public:
47 enum { min_value = 0, max_value = 0x7FFFFFFF };
48 typedef int32_t value_type;
49 kvrandom_lcg_nr(seed_type seed) : kvrandom_lcg_nr_simple(seed) {}
51 uint32_t x0 = kvrandom_lcg_nr_simple::next(),
52 x1 = kvrandom_lcg_nr_simple::next();
53 return (x0 >> 15) | ((x1 & 0x7FFE) << 16);
57 // A random number generator taken from NR's ran4. Based on hashing.
58 class kvrandom_psdes_nr { public:
59 enum { min_value = 0, max_value = 0xFFFFFFFFU };
60 typedef uint32_t value_type;
61 typedef uint32_t seed_type;
65 explicit kvrandom_psdes_nr(seed_type seed) {
68 void reset(seed_type seed) {
73 uint32_t value = psdes(seed_, next_);
77 value_type operator[](uint32_t index) const {
78 return psdes(seed_, index);
84 static const uint32_t c1[niter], c2[niter];
85 static uint32_t psdes(uint32_t lword, uint32_t irword);
88 // a wrapper around random(), for backwards compatibility
89 class kvrandom_random { public:
92 void reset(uint32_t seed) {
95 int32_t next() const {