Arcane  v3.16.8.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
LinearCongruential.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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/* LinearCongruential.h (C) 2000-2025 */
9/* */
10/* Randomiser 'LinearCongruential'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_CORE_RANDOM_LINEARCONGRUENTIAL_H
14#define ARCANE_CORE_RANDOM_LINEARCONGRUENTIAL_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/FatalErrorException.h"
19#include "arcane/utils/TraceInfo.h"
20#include "arcane/utils/String.h"
21
22#include "arcane/core/random/RandomGlobal.h"
23#include "arcane/core/random/ConstMod.h"
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane::random
29{
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
41template<typename IntType, IntType a, IntType c, IntType m, IntType val>
42class LinearCongruential
43{
44 public:
45 typedef IntType result_type;
46 static const bool has_fixed_range = true;
47 static const result_type min_value = ( c == 0 ? 1 : 0 );
48 static const result_type max_value = m-1;
49 static const IntType multiplier = a;
50 static const IntType increment = c;
51 static const IntType modulus = m;
52
53 result_type min() const { return c == 0 ? 1 : 0; }
54 result_type max() const { return m-1; }
55 explicit LinearCongruential(IntType x0 = 1)
56 : _x(x0)
57 {
58#ifdef ARCANE_CHECK
59 checkSeed(_x);
60#endif
61 }
62 // compiler-generated copy constructor and assignment operator are fine
63 void seed(IntType x0)
64 {
65 _x = x0;
66#ifdef ARCANE_CHECK
67 checkSeed(_x);
68#endif
69 }
70 IntType getState() const { return _x; }
71 IntType operator()()
72 {
73 _x = apply(_x);
74 return _x;
75 }
76 static IntType apply(IntType x)
77 {
78 return x = utils::const_mod<IntType, m>::mult_add(a, x, c);
79 }
80 bool validation(IntType x) const { return val == x; }
81 bool operator==(const LinearCongruential& rhs) const
82 { return _x == rhs._x; }
83
84 inline void checkSeed(IntType x)
85 {
86 if (_x<min() || _x>=max())
87 throw FatalErrorException(A_FUNCINFO,String::format("Invalid seed v={0} min={1} max={2}",x,min(),max()));
88 }
89
90 private:
91
92 IntType _x;
93};
94
95/*---------------------------------------------------------------------------*/
96/*---------------------------------------------------------------------------*/
97
100
101/*---------------------------------------------------------------------------*/
102/*---------------------------------------------------------------------------*/
103
104}
105
106/*---------------------------------------------------------------------------*/
107/*---------------------------------------------------------------------------*/
108
109#endif
Exception lorsqu'une erreur fatale est survenue.
compile-time configurable linear congruential generator.