Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
LinearCongruential.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/* LinearCongruential.h (C) 2000-2025 */
9/* */
10/* Randomize '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
46 typedef IntType result_type;
47 static const bool has_fixed_range = true;
48 static const result_type min_value = (c == 0 ? 1 : 0);
49 static const result_type max_value = m - 1;
50 static const IntType multiplier = a;
51 static const IntType increment = c;
52 static const IntType modulus = m;
53
54 result_type min() const { return c == 0 ? 1 : 0; }
55 result_type max() const { return m - 1; }
56 explicit LinearCongruential(IntType x0 = 1)
57 : _x(x0)
58 {
59#ifdef ARCANE_CHECK
60 checkSeed(_x);
61#endif
62 }
63 // compiler-generated copy constructor and assignment operator are fine
64 void seed(IntType x0)
65 {
66 _x = x0;
67#ifdef ARCANE_CHECK
68 checkSeed(_x);
69#endif
70 }
71 IntType getState() const { return _x; }
72 IntType operator()()
73 {
74 _x = apply(_x);
75 return _x;
76 }
77 static IntType apply(IntType x)
78 {
79 return x = utils::const_mod<IntType, m>::mult_add(a, x, c);
80 }
81 bool validation(IntType x) const { return val == x; }
82 bool operator==(const LinearCongruential& rhs) const
83 {
84 return _x == rhs._x;
85 }
86
87 inline void checkSeed(IntType x)
88 {
89 if (_x < min() || _x >= max())
90 throw FatalErrorException(A_FUNCINFO, String::format("Invalid seed v={0} min={1} max={2}", x, min(), max()));
91 }
92
93 private:
94
95 IntType _x;
96};
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
103
104/*---------------------------------------------------------------------------*/
105/*---------------------------------------------------------------------------*/
106
107} // namespace Arcane::random
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
111
112#endif
compile-time configurable linear congruential generator.