Rand48.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2018-2020 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26Class
27 Foam::Rand48
28
29Description
30 A pseudo random number generator using the linear congruential algorithm
31 with the following parameters:
32 \verbatim
33 Xn+1 = (aXn + c) mod m, where n >= 0
34
35 a = 0x5DEECE66D,
36 c = 0xB
37 m = 2**48
38 \endverbatim
39
40 It delivers results identical to the \c lrand48() function that is
41 available on some systems.
42
43\*---------------------------------------------------------------------------*/
44
45#ifndef Rand48_H
46#define Rand48_H
47
48#include <cstdint>
49#include <random>
50
51// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52
53namespace Foam
54{
55
56/*---------------------------------------------------------------------------*\
57 Class Rand48 Declaration
58\*---------------------------------------------------------------------------*/
60class Rand48
61{
62 //- It is a linear_congruential_engine
63 typedef std::linear_congruential_engine
64 <
65 uint64_t,
66 0x5DEECE66D,
67 0xB,
68 (uint64_t(1) << 48)
69 > engine;
70
71
72 // Private Data
73
74 //- The calculation engine
75 engine engine_;
76
77
78 // Private Member Functions
79
80 //- Convert integers
81 static constexpr uint64_t convert(uint32_t x) noexcept
82 {
83 return (static_cast<uint64_t>(x) << 16) | 0x330e;
84 }
85
86public:
87
88 //- The type of the generated random value.
89 typedef uint32_t result_type;
90
91 //- The default seed
92 static constexpr result_type default_seed = 1u;
93
94
95 // Constructors
96
97 //- Construct with specified or default seed
98 explicit Rand48(uint32_t val = default_seed)
99 :
100 engine_(convert(val))
101 {}
102
103
104 // Member Functions
105
106 //- The smallest value that the generator can produce
107 static constexpr uint32_t min() { return 0; }
108
109 //- The largest value that the generator can produce
110 static constexpr uint32_t max() { return 0x7FFFFFFF; }
111
112 //- Reseed the generator
113 void seed(uint32_t val = default_seed)
114 {
115 engine_.seed(convert(val));
116 }
117
118 //- Advance the state of the generator by \c z notches.
119 void discard(unsigned long long z)
120 {
121 engine_.discard(z);
122 }
123
124 //- Get the next random number in the sequence and return its raw
125 //- 48-bit value
126 uint64_t raw()
127 {
128 return engine_();
129 }
130
131
132 // Member Operators
133
134 //- Get the next random number in the sequence
135 uint32_t operator()()
136 {
137 return static_cast<uint32_t>(engine_() >> 17);
138 }
139};
140
141// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
142
143} // End namespace Foam
144
145#endif
146
147// ************************************************************************* //
A pseudo random number generator using the linear congruential algorithm with the following parameter...
Definition: Rand48.H:60
void seed(uint32_t val=default_seed)
Reseed the generator.
Definition: Rand48.H:112
static constexpr uint32_t min()
The smallest value that the generator can produce.
Definition: Rand48.H:106
void discard(unsigned long long z)
Advance the state of the generator by z notches.
Definition: Rand48.H:118
uint64_t raw()
Definition: Rand48.H:125
uint32_t result_type
The type of the generated random value.
Definition: Rand48.H:88
Rand48(uint32_t val=default_seed)
Construct with specified or default seed.
Definition: Rand48.H:97
static constexpr uint32_t max()
The largest value that the generator can produce.
Definition: Rand48.H:109
static constexpr result_type default_seed
The default seed.
Definition: Rand48.H:91
uint32_t operator()()
Get the next random number in the sequence.
Definition: Rand48.H:134
Namespace for OpenFOAM.