Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
TMrg32k3a.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/* Mrg32k3a.h (C) 2000-2025 */
9/* */
10/* This file defines the class template TMrg32k3a as well as the associated */
11/* class Mrg32k3a. */
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_CORE_RANDOM_MRG32K3A_H
14#define ARCANE_CORE_RANDOM_MRG32K3A_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/FatalErrorException.h"
19
20#include "arcane/core/random/RandomGlobal.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane::random
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
48template <typename RealType, Int32 val>
50{
51 public:
52
53 typedef RealType result_type;
54 typedef RealType state_type;
55 static const bool has_fixed_range = true;
56 static const Int32 min_value = 0;
57 static const Int32 max_value = 1;
58
59 /*---------------------------------------------------------------------------*/
60 /*---------------------------------------------------------------------------*/
61
68 explicit TMrg32k3a(Int32 x0 = 1)
69 {
70 seed(x0);
71 }
72
73 /*---------------------------------------------------------------------------*/
74 /*---------------------------------------------------------------------------*/
75
82 explicit TMrg32k3a(state_type* state)
83 {
84 for (Integer i = 0; i < 6; i++)
85 _state[i] = state[i];
86 }
87
88 /*---------------------------------------------------------------------------*/
89 /*---------------------------------------------------------------------------*/
90
97 void seed(Int32 x0)
98 {
99 x0 = (x0 | 1);
100 _state[0] = (state_type)x0;
101 _state[1] = _state[0];
102 _state[2] = _state[1];
103 _state[3] = _state[2];
104 _state[4] = _state[3];
105 _state[5] = _state[4];
106 }
107
108 /*---------------------------------------------------------------------------*/
109 /*---------------------------------------------------------------------------*/
110
118 RealType getState(Integer i) const { return _state[i]; }
119
120 /*---------------------------------------------------------------------------*/
121 /*---------------------------------------------------------------------------*/
122
129 RealType operator()()
130 {
131 RealType _x;
132 _x = apply(_state);
133 return _x;
134 }
135
136 /*---------------------------------------------------------------------------*/
137 /*---------------------------------------------------------------------------*/
138
145 static RealType apply(state_type* state)
146 {
147 long k;
148 Real p;
149 p = 1403580.0 * state[1] - 810728.0 * state[0];
150 k = static_cast<long>(p / 4294967087.0);
151 p -= k * 4294967087.0;
152 if (p < 0.0)
153 p += 4294967087.0;
154 state[0] = state[1];
155 state[1] = state[2];
156 state[2] = p;
157
158 p = 527612.0 * state[5] - 1370589.0 * state[3];
159 k = static_cast<long>(p / 4294944443.0);
160 p -= k * 4294944443.0;
161 if (p < 0.0)
162 p += 4294944443.0;
163 state[3] = state[4];
164 state[4] = state[5];
165 state[5] = p;
166
167 if (state[2] <= state[5])
168 return ((state[2] - state[5] + 4294967087.0) / 4294967087.0);
169 else
170 return ((state[2] - state[5]) / 4294967087.0);
171 }
172
173 /*---------------------------------------------------------------------------*/
174 /*---------------------------------------------------------------------------*/
175
181 result_type min() const { return static_cast<result_type>(min_value); }
182
183 /*---------------------------------------------------------------------------*/
184 /*---------------------------------------------------------------------------*/
185
191 result_type max() const { return static_cast<result_type>(max_value); }
192
193 /*---------------------------------------------------------------------------*/
194 /*---------------------------------------------------------------------------*/
195
201 bool validation(RealType x) const { return val == x; }
202
203 /*---------------------------------------------------------------------------*/
204 /*---------------------------------------------------------------------------*/
205
211 bool operator==(const TMrg32k3a& rhs) const
212 {
213 return (_state[0] == rhs._state[0]) && (_state[1] == rhs._state[1]) && (_state[2] == rhs._state[2]) && (_state[3] == rhs._state[3]) && (_state[4] == rhs._state[4]) && (_state[5] == rhs._state[5]);
214 }
215
216 private:
217
218 state_type _state[6];
219};
220
221/*---------------------------------------------------------------------------*/
222/*---------------------------------------------------------------------------*/
223
224typedef TMrg32k3a<Real, 0> Mrg32k3a;
225
226/*---------------------------------------------------------------------------*/
227/*---------------------------------------------------------------------------*/
228
229} // namespace Arcane::random
230
231/*---------------------------------------------------------------------------*/
232/*---------------------------------------------------------------------------*/
233
234#endif
bool operator==(const TMrg32k3a &rhs) const
Overloading of the == operator.
Definition TMrg32k3a.h:211
static RealType apply(state_type *state)
Returns the pseudo-random value from the state. The generator state state must consist of six element...
Definition TMrg32k3a.h:145
result_type max() const
Returns the maximum possible value of a sequence.
Definition TMrg32k3a.h:191
result_type min() const
Returns the minimum possible value of a sequence.
Definition TMrg32k3a.h:181
RealType getState(Integer i) const
Method that returns the generator state for index i. The complete state of the generator is given by ...
Definition TMrg32k3a.h:118
TMrg32k3a(state_type *state)
Constructor initializing the seed array from the state array. state must be an array of six elements.
Definition TMrg32k3a.h:82
void seed(Int32 x0)
Initialization of the seed array from the value x0. The seed array of this generator consists of six ...
Definition TMrg32k3a.h:97
TMrg32k3a(Int32 x0=1)
Constructor initializing the seed array from the value x0. The seed(x0) method is called.
Definition TMrg32k3a.h:68
bool validation(RealType x) const
Validation function (I'm not sure what it's for!).
Definition TMrg32k3a.h:201
RealType operator()()
Overloading of the () operator which returns the pseudo random value of the generator....
Definition TMrg32k3a.h:129
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
std::int32_t Int32
Signed integer type of 32 bits.