Arcane
v3.14.10.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-2022 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-2015 */
9
/* */
10
/* Randomiser 'LinearCongruential'. */
11
/*---------------------------------------------------------------------------*/
12
/*---------------------------------------------------------------------------*/
13
#ifndef ARCANE_RANDOM_LINEARCONGRUENTIAL_H
14
#define ARCANE_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/random/RandomGlobal.h"
23
#include "arcane/random/ConstMod.h"
24
25
/*---------------------------------------------------------------------------*/
26
/*---------------------------------------------------------------------------*/
27
28
ARCANE_BEGIN_NAMESPACE
29
30
/*---------------------------------------------------------------------------*/
31
/*---------------------------------------------------------------------------*/
32
33
RANDOM_BEGIN_NAMESPACE
34
35
/*---------------------------------------------------------------------------*/
36
/*---------------------------------------------------------------------------*/
37
38
/*---------------------------------------------------------------------------*/
39
/*---------------------------------------------------------------------------*/
40
48
template
<
typename
IntType, IntType a, IntType c, IntType m, IntType val>
49
class
LinearCongruential
50
{
51
public
:
52
typedef
IntType result_type;
53
static
const
bool
has_fixed_range =
true
;
54
static
const
result_type min_value = (
c
== 0 ? 1 : 0 );
55
static
const
result_type max_value = m-1;
56
static
const
IntType multiplier = a;
57
static
const
IntType increment =
c
;
58
static
const
IntType modulus = m;
59
60
result_type min()
const
{
return
c
== 0 ? 1 : 0; }
61
result_type max()
const
{
return
m-1; }
62
explicit
LinearCongruential
(IntType
x0
= 1)
63
: _x(
x0
)
64
{
65
#ifdef ARCANE_CHECK
66
checkSeed(_x);
67
#endif
68
}
69
// compiler-generated copy constructor and assignment operator are fine
70
void
seed(IntType
x0
)
71
{
72
_x =
x0
;
73
#ifdef ARCANE_CHECK
74
checkSeed(_x);
75
#endif
76
}
77
IntType getState()
const
{
return
_x; }
78
IntType operator()()
79
{
80
_x = apply(_x);
81
return
_x;
82
}
83
static
IntType apply(IntType x)
84
{
85
return
x =
utils::const_mod<IntType, m>::mult_add
(a, x,
c
);
86
}
87
bool
validation(IntType x)
const
{
return
val
== x; }
88
bool
operator==(
const
LinearCongruential
&
rhs
)
const
89
{
return
_x ==
rhs
._x; }
90
91
inline
void
checkSeed(IntType x)
92
{
93
if
(_x<min() || _x>=max())
94
throw
FatalErrorException
(A_FUNCINFO,String::format(
"Invalid seed v={0} min={1} max={2}"
,x,min(),max()));
95
}
96
97
private
:
98
99
IntType _x;
100
};
101
102
/*---------------------------------------------------------------------------*/
103
/*---------------------------------------------------------------------------*/
104
105
typedef
LinearCongruential<Int32,16807,0,2147483647,1043618065>
MinstdRand0
;
106
typedef
LinearCongruential<Int32,48271,0,2147483647,399268537>
MinstdRand
;
107
108
/*---------------------------------------------------------------------------*/
109
/*---------------------------------------------------------------------------*/
110
111
RANDOM_END_NAMESPACE
112
ARCANE_END_NAMESPACE
113
114
/*---------------------------------------------------------------------------*/
115
/*---------------------------------------------------------------------------*/
116
117
#endif
Arcane::LimaWrapper
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition
Lima.cc:120
Arcane::LinearCongruential
compile-time configurable linear congruential generator.
Definition
LinearCongruential.h:50
Arcane::utils::const_mod
Definition
RandomGlobal.h:139
Arccore::FatalErrorException
Exception lorsqu'une erreur fatale est survenue.
Definition
arccore/src/base/arccore/base/FatalErrorException.h:32
arcane
core
random
LinearCongruential.h
Généré le Lundi 18 Novembre 2024 03:01:04 pour Arcane par
1.9.8