Arcane  4.2.1.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-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
44class ARCANE_CORE_EXPORT RNGSeedHelper
45{
46 public:
47
54 {
55 m_seed = av;
56 }
57
65 template <class T>
67 {
68 m_seed = ByteArrayView(sizeof(T), (Byte*)var);
69 }
70
71 virtual ~RNGSeedHelper() = default;
72
73 public:
74
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
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
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
144 {
145 return m_seed.size();
146 }
147
154 {
155 return m_seed.constView();
156 }
157
164 {
165 return m_seed;
166 }
167
175 template <class T>
177 {
178 setValue(new_value);
179 return *this;
180 }
181
188 {
189 return ByteUniqueArray(m_seed);
190 }
191
192 protected:
193
194 ByteArrayView m_seed;
195};
196
201class ARCANE_CORE_EXPORT IRandomNumberGenerator
202{
203 public:
204
205 virtual ~IRandomNumberGenerator() = default;
206
207 public:
208
217 virtual bool initSeed() = 0;
218
228 virtual bool initSeed(ByteArrayView seed) = 0;
229
236
243
251
259 virtual bool isLeapSeedSupported() = 0;
260
268
279 virtual ByteUniqueArray generateRandomSeed(ByteArrayView parent_seed, Integer leap = 0) = 0;
280
288 virtual bool isLeapNumberSupported() = 0;
289
296 virtual Real generateRandomNumber(Integer leap = 0) = 0;
297
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