libfoedus-core
FOEDUS Core Library
uniform_random.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_ASSORTED_UNIFORM_RANDOM_HPP_
19 #define FOEDUS_ASSORTED_UNIFORM_RANDOM_HPP_
20 
21 #include <stdint.h>
22 
23 #include "foedus/assert_nd.hpp"
24 #include "foedus/memory/fwd.hpp"
25 
26 namespace foedus {
27 namespace assorted {
36  public:
37  UniformRandom() : seed_(0) {}
38  explicit UniformRandom(uint64_t seed) : seed_(seed) {}
39 
44  uint32_t uniform_within(uint32_t from, uint32_t to) {
45  ASSERT_ND(from <= to);
46  if (from == to) {
47  return from;
48  }
49  return from + (next_uint32() % (to - from + 1));
50  }
55  uint32_t uniform_within_except(uint32_t from, uint32_t to, uint32_t except) {
56  while (true) {
57  uint32_t val = uniform_within(from, to);
58  if (val != except) {
59  return val;
60  }
61  }
62  }
63 
70  uint32_t non_uniform_within(uint32_t A, uint32_t from, uint32_t to) {
71  uint32_t C = get_c(A);
72  return (((uniform_within(0, A) | uniform_within(from, to)) + C) % (to - from + 1)) + from;
73  }
74 
75  uint64_t get_current_seed() const {
76  return seed_;
77  }
78  void set_current_seed(uint64_t seed) {
79  seed_ = seed;
80  }
81 
102 
103  uint64_t next_uint64() {
104  return (static_cast<uint64_t>(next_uint32()) << 32) | next_uint32();
105  }
106  uint32_t next_uint32() {
107  seed_ = seed_ * 0xD04C3175 + 0x53DA9022;
108  return (seed_ >> 32) ^ (seed_ & 0xFFFFFFFF);
109  }
110 
111  private:
112  uint64_t seed_;
113 
120  uint32_t get_c(uint32_t A) const {
121  // yes, I'm lazy. but this satisfies the spec.
122  const uint64_t kCSeed = 0x734b00c6d7d3bbdaULL;
123  return kCSeed % (A + 1);
124  }
125 };
126 
127 } // namespace assorted
128 } // namespace foedus
129 
130 #endif // FOEDUS_ASSORTED_UNIFORM_RANDOM_HPP_
uint32_t non_uniform_within(uint32_t A, uint32_t from, uint32_t to)
Non-Uniform random (NURand) in TPCC spec (see Sec 2.1.6).
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
uint32_t uniform_within_except(uint32_t from, uint32_t to, uint32_t except)
Same as uniform_within() except it avoids the "except" value.
void set_current_seed(uint64_t seed)
Forward declarations of classes in memory package.
uint32_t uniform_within(uint32_t from, uint32_t to)
In TPCC terminology, from=x, to=y.
A very simple and deterministic random generator that is more aligned with standard benchmark such as...
Represents one memory block aligned to actual OS/hardware pages.
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
void fill_memory(foedus::memory::AlignedMemory *memory)
Fill up the give memory with random data.