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 
36 namespace Utils {
37 
46  class Random {
47  public:
53  Random() = delete;
54 
61  Random(std::uint64_t seed = 0);
62 
69  void seedrandom(std::uint64_t s);
70 
71  // Random numbers from [0, max - 1]
72  template<int MAX>
79  std::uint32_t randfix() {
80  static_assert(0 < MAX && MAX < std::numeric_limits<std::uint32_t>::max(),"randfix out of range");
81  // Last bit isn't random, so don't use it in isolation. We specialize
82  // this case.
83  static_assert(MAX != 2, "don't isolate the LSB with xoroshiro128+");
84  return gen() % MAX;
85  }
86 
93  std::uint64_t randuint64();
94 
95  // Random number from [0, max - 1]
102  std::uint64_t randuint64(const std::uint64_t max);
103 
104  // return the thread local RNG
111  static Random& get_Rng(void);
112 
113  // UniformRandomBitGenerator interface
114  using result_type = std::uint64_t;
115 
122  constexpr static result_type min() {
123  return std::numeric_limits<result_type>::min();
124  }
125 
132  constexpr static result_type max() {
133  return std::numeric_limits<result_type>::max();
134  }
135 
143  return gen();
144  }
145 
146  private:
153  std::uint64_t gen(void);
154 
155  std::uint64_t m_s[2];
156  };
157 
158  // Specialization for last bit: use sign test
159  template<>
160  inline std::uint32_t Random::randfix<2>() {
161  return (gen() > (std::numeric_limits<std::uint64_t>::max() / 2));
162  }
163 }
164 
165 #endif
std::uint64_t randuint64()
Generate raw random number.
Definition: Random.cpp:70
std::uint64_t result_type
Definition: Random.h:114
static Random & get_Rng(void)
Get a copy of the current thread.
Definition: Random.cpp:35
RNG based on Xoroshiro128+.
Definition: Random.h:46
Random()=delete
Free the pointed object.
static constexpr result_type max()
Max integer value, part of UniformRandomBitGenerator interface.
Definition: Random.h:132
std::uint64_t m_s[2]
Definition: Random.h:155
static constexpr result_type min()
Min integer value, part of UniformRandomBitGenerator interface.
Definition: Random.h:122
std::uint32_t randfix()
Check that the random number will be generated in a correct range, then generate it.
Definition: Random.h:79
void seedrandom(std::uint64_t s)
Initialize state of xoroshiro128+ by transforming the seed with the splitmix64 algorithm.
Definition: Random.cpp:81
std::uint64_t gen(void)
Generate random generated number with Xoroshiro128+.
Definition: Random.cpp:53
result_type operator()()
Generate a raw random number, part of UniformRandomBitGenerator interface.
Definition: Random.h:142
A set of useful method and class.
Definition: Random.cpp:33