Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
InversiveCongruential.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* InversiveCongruential.h (C) 2000-2025 */
9/* */
10/* This file defines the InversiveCongruential class pattern as well as a */
11/* associated class Hellekalek1995. It is an adapted version of the file */
12/* InversiveCongruential.hpp from the BOOST library */
13/*---------------------------------------------------------------------------*/
14/*---------------------------------------------------------------------------*/
15#ifndef ARCANE_CORE_RANDOM_INVERSIVECONGRUENTIAL_H
16#define ARCANE_CORE_RANDOM_INVERSIVECONGRUENTIAL_H
17/*---------------------------------------------------------------------------*/
18/*---------------------------------------------------------------------------*/
19
20#include "arcane/utils/FatalErrorException.h"
21
22#include "arcane/core/random/RandomGlobal.h"
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane::random
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
48template <typename IntType, IntType a, IntType c, IntType m, IntType val>
50{
51 public:
52
53 typedef IntType result_type;
54 static const bool has_fixed_range = true;
55 static const result_type min_value = (c == 0 ? 1 : 0);
56 static const result_type max_value = m - 1;
57
58 /*---------------------------------------------------------------------------*/
59 /*---------------------------------------------------------------------------*/
60
66 result_type min() const { return c == 0 ? 1 : 0; }
67
68 /*---------------------------------------------------------------------------*/
69 /*---------------------------------------------------------------------------*/
70
76 result_type max() const { return m - 1; }
77
78 /*---------------------------------------------------------------------------*/
79 /*---------------------------------------------------------------------------*/
80
87 explicit InversiveCongruential(IntType x0 = 1)
88 : _x(x0)
89 {
90 }
91
92 /*---------------------------------------------------------------------------*/
93 /*---------------------------------------------------------------------------*/
94
100 void seed(IntType x0) { _x = x0; }
101
102 /*---------------------------------------------------------------------------*/
103 /*---------------------------------------------------------------------------*/
104
110 IntType getState() const { return _x; }
111
112 /*---------------------------------------------------------------------------*/
113 /*---------------------------------------------------------------------------*/
114
121 IntType operator()()
122 {
123 _x = apply(_x);
124 return _x;
125 }
126
127 /*---------------------------------------------------------------------------*/
128 /*---------------------------------------------------------------------------*/
129
137 static IntType apply(IntType x)
138 {
139 typedef utils::const_mod<IntType, m> do_mod;
140 return x = do_mod::mult_add(a, do_mod::invert(x), c);
141 }
142
143 /*---------------------------------------------------------------------------*/
144 /*---------------------------------------------------------------------------*/
145
151 bool validation(IntType x) const { return val == x; }
152
153 /*---------------------------------------------------------------------------*/
154 /*---------------------------------------------------------------------------*/
155
161 bool operator==(const InversiveCongruential& rhs) const
162 {
163 return _x == rhs._x;
164 }
165
166 private:
167
168 IntType _x;
169};
170
171/*---------------------------------------------------------------------------*/
172/*---------------------------------------------------------------------------*/
173
174typedef InversiveCongruential<Int32, 9102, 2147483647 - 36884165,
175 2147483647, 0>
176Hellekalek1995;
177
178/*---------------------------------------------------------------------------*/
179/*---------------------------------------------------------------------------*/
180
181} // namespace Arcane::random
182
183/*---------------------------------------------------------------------------*/
184/*---------------------------------------------------------------------------*/
185
186#endif
result_type max() const
Returns the maximum possible value of a sequence.
result_type min() const
Returns the minimum possible value of a sequence.
IntType operator()()
Overriding the operator () which returns the pseudo random value of the generator....
bool validation(IntType x) const
Validation function (I don't really know what it is for!).
static IntType apply(IntType x)
Returns the pseudo-random value from the state x. The private member _x of the generator is not used ...
void seed(IntType x0)
Initialization of the generator seed from the value x0.
bool operator==(const InversiveCongruential &rhs) const
Overriding the == operator.
IntType getState() const
Method that returns the generator state.
InversiveCongruential(IntType x0=1)
Constructor with seed initialization from the value x0.
std::int32_t Int32
Signed integer type of 32 bits.