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 
25 /*
26  Random number generator xoroshiro128+
27 */
28 namespace Utils
29 {
30 class Random {
31 public:
32  Random() = delete;
33  Random(std::uint64_t seed = 0);
34  void seedrandom(std::uint64_t s);
35 
36  // Random numbers from [0, max - 1]
37  template<int MAX>
38  std::uint32_t randfix() {
39  static_assert(0 < MAX &&
40  MAX < std::numeric_limits<std::uint32_t>::max(),
41  "randfix out of range");
42  // Last bit isn't random, so don't use it in isolation. We specialize
43  // this case.
44  static_assert(MAX != 2, "don't isolate the LSB with xoroshiro128+");
45  return gen() % MAX;
46  }
47 
48  std::uint64_t randuint64();
49 
50  // Random number from [0, max - 1]
51  std::uint64_t randuint64(const std::uint64_t max);
52 
53  // return the thread local RNG
54  static Random& get_Rng(void);
55 
56  // UniformRandomBitGenerator interface
57  using result_type = std::uint64_t;
58  constexpr static result_type min() {
59  return std::numeric_limits<result_type>::min();
60  }
61  constexpr static result_type max() {
62  return std::numeric_limits<result_type>::max();
63  }
65  return gen();
66  }
67 
68 private:
69  std::uint64_t gen(void);
70  std::uint64_t m_s[2];
71 };
72 
73 // Specialization for last bit: use sign test
74 template<>
75 inline std::uint32_t Random::randfix<2>() {
76  return (gen() > (std::numeric_limits<std::uint64_t>::max() / 2));
77 }
78 }
79 #endif
static constexpr result_type max()
Definition: Random.h:61
static Random & get_Rng(void)
Definition: Random.cpp:30
std::uint64_t result_type
Definition: Random.h:57
Definition: Random.h:30
static constexpr result_type min()
Definition: Random.h:58
Random()=delete
std::uint64_t randuint64()
Definition: Random.cpp:65
std::uint32_t randfix()
Definition: Random.h:38
result_type operator()()
Definition: Random.h:64
void seedrandom(std::uint64_t s)
Definition: Random.cpp:76
A set of useful method and class.
Definition: Random.cpp:27