scc  2022.4.0
SystemC components library
mt19937_rng.h
1 /*******************************************************************************
2  * Copyright 2020-2022 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #ifndef _UTIL_MT19937_RNG_H_
18 #define _UTIL_MT19937_RNG_H_
19 
20 #include <cassert>
21 #include <iostream>
22 #include <random>
23 
29 namespace util {
31 class MT19937 {
32 public:
37  static void seed(uint64_t new_seed = std::mt19937_64::default_seed) { inst().seed(new_seed); }
42  static uint64_t uniform() {
43  std::uniform_int_distribution<uint64_t> u;
44  return u(inst());
45  }
50  template <typename T> static T uniform() {
51  std::uniform_int_distribution<T> u;
52  return u(inst());
53  }
60  static uint64_t uniform(uint64_t min, uint64_t max) {
61  assert(min < max);
62  std::uniform_int_distribution<uint64_t> u(min, max);
63  return u(inst());
64  }
69  static double normal() {
70  std::normal_distribution<> u;
71  return u(inst());
72  }
77  static double lognormal() {
78  std::lognormal_distribution<> u;
79  return u(inst());
80  }
81 
82 private:
83  static std::mt19937_64& inst() {
84  static thread_local std::mt19937_64 rng;
85  return rng;
86  }
87 };
88 } // namespace util
90 #endif /* _UTIL_MT19937_RNG_H_ */
a Mersenne-Twister pseudo random number generator
Definition: mt19937_rng.h:31
static T uniform()
Definition: mt19937_rng.h:50
static void seed(uint64_t new_seed=std::mt19937_64::default_seed)
Definition: mt19937_rng.h:37
static double lognormal()
Definition: mt19937_rng.h:77
static uint64_t uniform()
Definition: mt19937_rng.h:42
static double normal()
Definition: mt19937_rng.h:69
static uint64_t uniform(uint64_t min, uint64_t max)
Definition: mt19937_rng.h:60
SCC common utilities.
Definition: bit_field.h:30