Arcane  4.2.1.0
User documentation
Loading...
Searching...
No Matches
IRandomNumberGenerator.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/* IRandomNumberGenerator.h (C) 2000-2026 */
9/* */
10/* Interface for random number generator. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_IRANDOMNUMBERGENERATOR_H
13#define ARCANE_CORE_IRANDOMNUMBERGENERATOR_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Array.h"
19
20#include <cstring>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31/**
32 * @brief Class allowing easy manipulation of a seed.
33 *
34 * A seed is represented by an array of Bytes.
35 * This class uses an ArrayView of this array.
36 *
37 * This class allows defining a value in the array and
38 * retrieving that value (other things).
39 *
40 * This class does not store the array but only
41 * an ArrayView of this array.
42 *
43 */
44class ARCANE_CORE_EXPORT RNGSeedHelper
45{
46 public:
47
48 /**
49 * @brief Class constructor.
50 *
51 * @param av An ArrayView of an array representing a seed.
52 */
54 {
55 m_seed = av;
56 }
57
58 /**
59 * @brief Class constructor.
60 *
61 * @tparam T A base type.
62 * @param var A pointer to the seed
63 * (note, does not make a copy of the value!).
64 */
65 template <class T>
67 {
68 m_seed = ByteArrayView(sizeof(T), (Byte*)var);
69 }
70
71 virtual ~RNGSeedHelper() = default;
72
73 public:
74
75 /**
76 * @brief Method allowing setting a value in the seed.
77 *
78 * @tparam T The value type.
79 * @param value_in The future value of the seed.
80 * @return true If the value could be assigned.
81 * @return false If the value could not be assigned.
82 */
83 template <class T>
84 bool setValue(T value_in)
85 {
86 if (m_seed.empty()) {
87 return false;
88 }
89 memcpy(m_seed.data(), &value_in, std::min(m_seed.size(), (Integer)sizeof(T)));
90 for (Integer i = sizeof(T); i < m_seed.size(); i++) {
91 m_seed[i] = 0x00;
92 }
93 return true;
94 }
95
96 /**
97 * @brief Method allowing retrieval of the seed value.
98 *
99 * @tparam T The seed type.
100 * @param value_out [OUT] The seed value.
101 * @param without_size_check If value truncation is allowed.
102 * @return true If the value could be retrieved.
103 * @return false If the value could not be retrieved or if the array
104 * has a null size.
105 */
106 template <class T>
107 bool value(T& value_out, bool without_size_check = true) const
108 {
109 if (m_seed.empty() || (!without_size_check && sizeof(T) != m_seed.size())) {
110 return false;
111 }
112 value_out = 0;
113 std::memcpy(&value_out, m_seed.data(), std::min(m_seed.size(), (Integer)sizeof(T)));
114 return true;
115 }
116
117 /**
118 * @brief Method allowing retrieval of the seed value.
119 *
120 * @tparam T The seed type.
121 * @param value_out [OUT] The seed value.
122 * @param without_size_check If value truncation is allowed.
123 * @return true If the value could be retrieved.
124 * @return false If the value could not be retrieved or if the array
125 * has a null size.
126 */
127 template <class T>
128 bool value(T* value_out, bool without_size_check = true) const
129 {
130 if (m_seed.empty() || (sizeof(T) != m_seed.size() && !without_size_check)) {
131 return false;
132 }
133 *value_out = 0;
134 memcpy(value_out, m_seed.data(), std::min(m_seed.size(), (Integer)sizeof(T)));
135 return true;
136 }
137
138 /**
139 * @brief Method allowing retrieval of the seed size.
140 *
141 * @return Integer The seed size (in bytes).
142 */
144 {
145 return m_seed.size();
146 }
147
148 /**
149 * @brief Method allowing retrieval of a constant view.
150 *
151 * @return ByteConstArrayView The view.
152 */
154 {
155 return m_seed.constView();
156 }
157
158 /**
159 * @brief Method allowing retrieval of a view.
160 *
161 * @return ByteArrayView The view.
162 */
164 {
165 return m_seed;
166 }
167
168 /**
169 * @brief Copy operator from a seed value.
170 *
171 * @tparam T The seed type.
172 * @param value The seed value.
173 * @return RNGSeedHelper& The destination seed.
174 */
175 template <class T>
177 {
178 setValue(new_value);
179 return *this;
180 }
181
182 /**
183 * @brief Method allowing retrieval of a copy of the Byte array.
184 *
185 * @return ByteUniqueArray The copy of the Byte array.
186 */
188 {
189 return ByteUniqueArray(m_seed);
190 }
191
192 protected:
193
194 ByteArrayView m_seed;
195};
196
197/**
198 * @ingroup StandardService
199 * @brief Interface for a random number generator.
200 */
201class ARCANE_CORE_EXPORT IRandomNumberGenerator
202{
203 public:
204
205 virtual ~IRandomNumberGenerator() = default;
206
207 public:
208
209 /**
210 * @brief Method allowing initialization of the service.
211 *
212 * With the seed optional (or the default seed if in singleton mode).
213 *
214 * @return true If initialization was successful.
215 * @return false If initialization did not occur.
216 */
217 virtual bool initSeed() = 0;
218
219 /**
220 * @brief Method allowing initialization of the service.
221 *
222 * If the seed does not have the correct size, false will be returned.
223 *
224 * @param seed The original seed.
225 * @return true If initialization was successful.
226 * @return false If initialization did not occur.
227 */
228 virtual bool initSeed(ByteArrayView seed) = 0;
229
230 /**
231 * @brief Method allowing retrieval of a constant view of the current seed.
232 *
233 * @return ByteArrayView The seed.
234 */
236
237 /**
238 * @brief Method allowing retrieval of an empty seed of the correct size.
239 *
240 * @return ByteUniqueArray The empty seed.
241 */
243
244 /**
245 * @brief Method allowing knowledge of the seed size required
246 * for the implementation.
247 *
248 * @return Integer The required seed size (in bytes).
249 */
251
252 /**
253 * @brief Method allowing knowledge if leaps are allowed on the
254 * seed generator.
255 *
256 * @return true If yes.
257 * @return false If no.
258 */
259 virtual bool isLeapSeedSupported() = 0;
260
261 /**
262 * @brief Method allowing generation of a "child" seed from a "parent" seed.
263 *
264 * @param leap The leap to perform (0 = seed n+1+0 / 1 = seed n+1+1).
265 * @return ByteUniqueArray The new seed generated from the seed in memory.
266 */
268
269 /**
270 * @brief Method allowing generation of a "child" seed from a "parent" seed.
271 *
272 * This method does not use the seed in memory but the seed provided as a parameter.
273 * If the seed provided as a parameter does not have the correct size, an error will be raised.
274 *
275 * @param parent_seed The "parent" seed.
276 * @param leap The leap to perform (0 = seed n+1+0 / 1 = seed n+1+1).
277 * @return ByteUniqueArray The new seed generated from the "parent" seed.
278 */
279 virtual ByteUniqueArray generateRandomSeed(ByteArrayView parent_seed, Integer leap = 0) = 0;
280
281 /**
282 * @brief Method allowing knowledge if leaps are allowed on the
283 * number generator.
284 *
285 * @return true If yes.
286 * @return false If no.
287 */
288 virtual bool isLeapNumberSupported() = 0;
289
290 /**
291 * @brief Method allowing generation of a random number using the seed in memory.
292 *
293 * @param leap The leap to perform (0 = number n+1+0 / 1 = number n+1+1).
294 * @return Real The generated number (between 0 and 1).
295 */
296 virtual Real generateRandomNumber(Integer leap = 0) = 0;
297
298 /**
299 * @brief Method allowing generation of a random number using the seed passed as a parameter.
300 *
301 * This method does not use the seed in memory but the seed provided as a parameter.
302 * If the seed provided as a parameter does not have the correct size, an error will be raised.
303 *
304 * @param seed The seed.
305 * @param leap The leap to perform (0 = number n+1+0 / 1 = number n+1+1).
306 * @return Real The generated number (between 0 and 1).
307 */
308 virtual Real generateRandomNumber(ByteArrayView seed, Integer leap = 0) = 0;
309};
310
311/*---------------------------------------------------------------------------*/
312/*---------------------------------------------------------------------------*/
313
314} // End namespace Arcane
315
316/*---------------------------------------------------------------------------*/
317/*---------------------------------------------------------------------------*/
318
319#endif
Declarations of types used in Arcane.
Interface for a random number generator.
virtual ByteConstArrayView viewSeed()=0
Method allowing retrieval of a constant view of the current seed.
virtual ByteUniqueArray emptySeed()=0
Method allowing retrieval of an empty seed of the correct size.
virtual Real generateRandomNumber(ByteArrayView seed, Integer leap=0)=0
Method allowing generation of a random number using the seed passed as a parameter.
virtual ByteUniqueArray generateRandomSeed(Integer leap=0)=0
Method allowing generation of a "child" seed from a "parent" seed.
virtual bool isLeapSeedSupported()=0
Method allowing knowledge if leaps are allowed on the seed generator.
virtual Real generateRandomNumber(Integer leap=0)=0
Method allowing generation of a random number using the seed in memory.
virtual ByteUniqueArray generateRandomSeed(ByteArrayView parent_seed, Integer leap=0)=0
Method allowing generation of a "child" seed from a "parent" seed.
virtual bool isLeapNumberSupported()=0
Method allowing knowledge if leaps are allowed on the number generator.
virtual bool initSeed(ByteArrayView seed)=0
Method allowing initialization of the service.
virtual Integer neededSizeOfSeed()=0
Method allowing knowledge of the seed size required for the implementation.
virtual bool initSeed()=0
Method allowing initialization of the service.
Class allowing easy manipulation of a seed.
bool value(T &value_out, bool without_size_check=true) const
Method allowing retrieval of the seed value.
Integer sizeOfSeed() const
Method allowing retrieval of the seed size.
bool setValue(T value_in)
Method allowing setting a value in the seed.
ByteConstArrayView constView() const
Method allowing retrieval of a constant view.
ByteUniqueArray copy()
Method allowing retrieval of a copy of the Byte array.
bool value(T *value_out, bool without_size_check=true) const
Method allowing retrieval of the seed value.
ByteArrayView view()
Method allowing retrieval of a view.
RNGSeedHelper(T *var)
Class constructor.
RNGSeedHelper(ByteArrayView av)
Class constructor.
RNGSeedHelper & operator=(T new_value)
Copy operator from a seed value.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
ArrayView< Byte > ByteArrayView
C equivalent of a 1D array of characters.
Definition UtilsTypes.h:441
Int32 Integer
Type representing an integer.
UniqueArray< Byte > ByteUniqueArray
Dynamic 1D array of characters.
Definition UtilsTypes.h:329
double Real
Type representing a real number.
ConstArrayView< Byte > ByteConstArrayView
C equivalent of a 1D array of characters.
Definition UtilsTypes.h:470
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:42