IAtari
Genetic algorithm generating AI capable to play Atari2600 games.
Random.h
Go to the documentation of this file.
1 /*
2  This file is part of Leela Zero.
3  Copyright (C) 2017 Gian-Carlo Pascutto
4 
5  Leela Zero is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Leela Zero is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Leela Zero. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef RANDOM_H_INCLUDED
20 #define RANDOM_H_INCLUDED
21 
22 #include <cstdint>
23 #include <limits>
24 
33 namespace Utils {
34 
40  class Random {
41  public:
47  Random() = delete;
48 
55  Random(std::uint64_t seed = 0);
56 
63  void seedrandom(std::uint64_t s);
64 
65  // Random numbers from [0, max - 1]
66  template<int MAX>
73  std::uint32_t randfix() {
74  static_assert(0 < MAX && MAX < std::numeric_limits<std::uint32_t>::max(),"randfix out of range");
75  // Last bit isn't random, so don't use it in isolation. We specialize
76  // this case.
77  static_assert(MAX != 2, "don't isolate the LSB with xoroshiro128+");
78  return gen() % MAX;
79  }
80 
87  std::uint64_t randuint64();
88 
89  // Random number from [0, max - 1]
96  std::uint64_t randuint64(const std::uint64_t max);
97 
98  // return the thread local RNG
105  static Random& get_Rng(void);
106 
107  // UniformRandomBitGenerator interface
108  using result_type = std::uint64_t;
109 
116  constexpr static result_type min() {
117  return std::numeric_limits<result_type>::min();
118  }
119 
126  constexpr static result_type max() {
127  return std::numeric_limits<result_type>::max();
128  }
129 
137  return gen();
138  }
139 
140  private:
147  std::uint64_t gen(void);
148 
149  std::uint64_t m_s[2];
150  };
151 
152  // Specialization for last bit: use sign test
153  template<>
154  inline std::uint32_t Random::randfix<2>() {
155  return (gen() > (std::numeric_limits<std::uint64_t>::max() / 2));
156  }
157 }
158 
159 #endif
std::uint64_t randuint64()
Generate raw random number.
Definition: Random.cpp:64
std::uint64_t result_type
Definition: Random.h:108
static Random & get_Rng(void)
Get a copy of the current thread.
Definition: Random.cpp:29
RNG based on Xoroshiro128+.
Definition: Random.h:40
Random()=delete
Free the pointed object.
static constexpr result_type max()
Max integer value, part of UniformRandomBitGenerator interface.
Definition: Random.h:126
static constexpr result_type min()
Min integer value, part of UniformRandomBitGenerator interface.
Definition: Random.h:116
std::uint32_t randfix()
Check that the random number will be generated in a correct range, then generate it.
Definition: Random.h:73
void seedrandom(std::uint64_t s)
Initialize state of xoroshiro128+ by transforming the seed with the splitmix64 algorithm.
Definition: Random.cpp:75
result_type operator()()
Generate a raw random number, part of UniformRandomBitGenerator interface.
Definition: Random.h:136
A set of useful method and class.
Definition: Random.cpp:27