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