libfoedus-core
FOEDUS Core Library
zipfian_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_ZIPFIAN_RANDOM_HPP_
19 #define FOEDUS_ASSORTED_ZIPFIAN_RANDOM_HPP_
20 
21 #include <stdint.h>
22 
23 #include <cmath>
24 
25 #include "foedus/assert_nd.hpp"
27 #include "foedus/memory/fwd.hpp"
28 
29 namespace foedus {
30 namespace assorted {
38  private:
39  double zeta(uint64_t n) {
40  double sum = 0;
41  for (uint64_t i = 0; i < n; i++) {
42  sum += 1 / std::pow(i + 1, theta_);
43  }
44  return sum;
45  }
46 
47  public:
48  void init(uint64_t items, double theta, uint64_t urnd_seed) {
49  max_ = items - 1;
50  theta_ = theta;
51  zetan_ = zeta(items);
52  alpha_ = 1.0 / (1.0 - theta_);
53  eta_ = (1 - std::pow(2.0 / items, 1 - theta_)) / (1 - zeta(2) / zetan_);
54  urnd_.set_current_seed(urnd_seed);
55  }
56 
57  ZipfianRandom(uint64_t items, double theta, uint64_t urnd_seed) {
58  init(items, theta, urnd_seed);
59  }
60 
62 
63  uint64_t next() {
64  double u = urnd_.uniform_within(0, max_) / static_cast<double>(max_);
65  double uz = u * zetan_;
66  if (uz < 1.0) {
67  return 0;
68  }
69 
70  if (uz < 1.0 + std::pow(0.5, theta_)) {
71  return 1;
72  }
73 
74  uint64_t ret = static_cast<uint64_t>(max_ * std::pow(eta_ * u - eta_ + 1, alpha_));
75  return ret;
76  }
77 
78  uint64_t get_current_seed() const { return urnd_.get_current_seed(); }
79  void set_current_seed(uint64_t seed) { urnd_.set_current_seed(seed); }
80 
81  private:
83  uint64_t max_;
84  uint64_t base_;
85  double theta_;
86  double zetan_;
87  double alpha_;
88  double eta_;
89 };
90 
91 } // namespace assorted
92 } // namespace foedus
93 
94 #endif // FOEDUS_ASSORTED_ZIPFIAN_RANDOM_HPP_
void set_current_seed(uint64_t seed)
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
void set_current_seed(uint64_t seed)
void init(uint64_t items, double theta, uint64_t urnd_seed)
Forward declarations of classes in memory package.
A simple zipfian generator based off of YCSB's Java implementation.
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...
ZipfianRandom(uint64_t items, double theta, uint64_t urnd_seed)