openGPMP
Open Source Mathematics Package
random.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  *
14  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
15  *
16  * This software is licensed as described in the file LICENSE, which
17  * you should have received as part of this distribution. The terms
18  * among other details are referenced in the official documentation
19  * seen here : https://akielaries.github.io/openGPMP/ along with
20  * important files seen in this project.
21  *
22  * You may opt to use, copy, modify, merge, publish, distribute
23  * and/or sell copies of the Software, and permit persons to whom
24  * the Software is furnished to do so, under the terms of the
25  * LICENSE file. As this is an Open Source effort, all implementations
26  * must be of the same methodology.
27  *
28  *
29  *
30  * This software is distributed on an AS IS basis, WITHOUT
31  * WARRANTY OF ANY KIND, either express or implied.
32  *
33  ************************************************************************/
38 #ifndef RANDOM_HPP
39 #define RANDOM_HPP
40 
41 #include <chrono>
42 #include <cstdint>
43 
45 #define __8MAX 127
46 #define __U8MAX 255
48 #define __16MAX 32767
49 #define __U16MAX 65535
51 #define __32MAX 2147483647L
52 #define __U32MAX 4294967295U
54 #define __64MAX 9223372036854775807LL
55 #define __U64MAX 18446744073709551615ULL
57 /*#define __PCG_STATE 0x4d595df4d0f33173
58 #define __PCG_MULTPLR 6364136223846793005u
59 #define __PCG_INCR 1442695040888963407u
60 */
61 static uint64_t __PCG_STATE = 0x4d595df4d0f33173; // Or something seed-dependent
62 static uint64_t const __PCG_MULTPLR = 6364136223846793005u;
63 static uint64_t const __PCG_INCR =
64  1442695040888963407u; // Or an arbitrary odd constant
65 
66 namespace gpmp {
67 
68 namespace core {
69 
73 namespace rndm {
74 
82 class LCG {
83  public:
84  using result_type = uint64_t;
85 
86  // Default constructor
87  LCG();
88 
89  // Constructor with parameters similar to std::linear_congruential_engine
90  LCG(uint64_t seed,
91  uint64_t a = 6364136223846793005ULL,
92  uint64_t c = 1442695040888963407ULL);
93 
94  // Function to generate a random number within a specific range
95  uint64_t operator()();
96 
97  // Function to set the seed
98  void seed(uint64_t new_seed);
99 
100  // Functions to retrieve parameters (optional)
101  uint64_t get_multiplier() const;
102  uint64_t get_increment() const;
103  uint64_t get_seed() const;
104 
105  private:
106  uint64_t state; // State of the generator
107  uint64_t multiplier; // Multiplier parameter
108  uint64_t increment; // Increment parameter
109 };
110 
111 uint32_t rotr32(uint32_t x, unsigned r);
112 
113 uint32_t pcg32(void);
114 
115 void pcg32_init(uint64_t seed);
116 
117 } // namespace rndm
118 
119 } // namespace core
120 
121 } // namespace gpmp
122 
123 #endif
Linear Congruential Generator.
Definition: random.hpp:82
uint64_t operator()()
Definition: random.cpp:49
uint64_t get_increment() const
Definition: random.cpp:75
void seed(uint64_t new_seed)
Definition: random.cpp:66
uint64_t result_type
Definition: random.hpp:84
uint64_t get_seed() const
Definition: random.cpp:79
uint64_t get_multiplier() const
Definition: random.cpp:71
void pcg32_init(uint64_t seed)
Definition: random.cpp:147
uint32_t rotr32(uint32_t x, unsigned r)
Definition: random.cpp:134
uint32_t pcg32(void)
Definition: random.cpp:138
The source C++ openGPMP namespace.
static uint64_t const __PCG_MULTPLR
Definition: random.hpp:62
static uint64_t __PCG_STATE
Definition: random.hpp:61
static uint64_t const __PCG_INCR
Definition: random.hpp:63