Arcane  4.1.12.0
Developer 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
45class ARCANE_CORE_EXPORT RNGSeedHelper
46{
47 public:
48
55 {
56 m_seed = av;
57 }
58
66 template <class T>
68 {
69 m_seed = ByteArrayView(sizeof(T), (Byte*)var);
70 }
71
72 virtual ~RNGSeedHelper() = default;
73
74 public:
75
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
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
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
145 {
146 return m_seed.size();
147 }
148
155 {
156 return m_seed.constView();
157 }
158
165 {
166 return m_seed;
167 }
168
176 template <class T>
178 {
179 setValue(new_value);
180 return *this;
181 }
182
189 {
190 return ByteUniqueArray(m_seed);
191 }
192
193 protected:
194
195 ByteArrayView m_seed;
196};
197
202class ARCANE_CORE_EXPORT IRandomNumberGenerator
203{
204 public:
205
206 virtual ~IRandomNumberGenerator() = default;
207
208 public:
209
218 virtual bool initSeed() = 0;
219
229 virtual bool initSeed(ByteArrayView seed) = 0;
230
237
244
252
260 virtual bool isLeapSeedSupported() = 0;
261
269
280 virtual ByteUniqueArray generateRandomSeed(ByteArrayView parent_seed, Integer leap = 0) = 0;
281
289 virtual bool isLeapNumberSupported() = 0;
290
297 virtual Real generateRandomNumber(Integer leap = 0) = 0;
298
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