Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
TKiss.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/* TKiss.h (C) 2000-2025 */
9/* */
10/* This file defines the TKiss class template and the associated class Kiss. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_RANDOM_TKISS_H
13#define ARCANE_CORE_RANDOM_TKISS_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/FatalErrorException.h"
18
19#include "arcane/core/random/RandomGlobal.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane::random
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
38template <typename UIntType, UIntType val>
39class TKiss
40{
41 public:
42
43 typedef UIntType result_type;
44 typedef UIntType state_type;
45 static const bool has_fixed_range = true;
46 static const result_type min_value = 0;
47 static const result_type max_value = 4294967295U;
48
49 /*---------------------------------------------------------------------------*/
50 /*---------------------------------------------------------------------------*/
51
57 result_type min() const { return min_value; }
58
59 /*---------------------------------------------------------------------------*/
60 /*---------------------------------------------------------------------------*/
61
67 result_type max() const { return max_value; }
68
69 /*---------------------------------------------------------------------------*/
70 /*---------------------------------------------------------------------------*/
71
77 explicit TKiss(UIntType x0 = 30903, UIntType y0 = 30903, UIntType z0 = 30903, UIntType w0 = 30903, UIntType carry0 = 0)
78 {
79 _state[0] = x0;
80 _state[1] = y0;
81 _state[2] = z0;
82 _state[3] = w0;
83 _state[4] = carry0;
84 }
85
86 /*---------------------------------------------------------------------------*/
87 /*---------------------------------------------------------------------------*/
88
95 void seed(UIntType* state)
96 {
97 for (Integer i = 0; i < 5; i++)
98 _state[i] = state[i];
99 }
100
101 /*---------------------------------------------------------------------------*/
102 /*---------------------------------------------------------------------------*/
103
112 void seed(UIntType x0)
113 {
114 for (Integer i = 0; i < 4; i++)
115 _state[i] = x0;
116 _state[4] = 0;
117 }
118
119 /*---------------------------------------------------------------------------*/
120 /*---------------------------------------------------------------------------*/
121
127 void seed(UIntType x0, UIntType y0, UIntType z0, UIntType w0, UIntType carry0)
128 {
129 _state[0] = x0;
130 _state[1] = y0;
131 _state[2] = z0;
132 _state[3] = w0;
133 _state[4] = carry0;
134 }
135
136 /*---------------------------------------------------------------------------*/
137 /*---------------------------------------------------------------------------*/
138
144 UIntType getState(Integer i) const { return _state[i]; }
145
146 /*---------------------------------------------------------------------------*/
147 /*---------------------------------------------------------------------------*/
148
154 UIntType operator()()
155 {
156 UIntType t;
157 _state[0] = _state[0] * 69069 + 1;
158 _state[1] ^= _state[1] << 13;
159 _state[1] ^= _state[1] >> 17;
160 _state[1] ^= _state[1] << 5;
161
162 t = (_state[3] << 1) + _state[2] + _state[4];
163 _state[4] = ((_state[2] >> 2) + (_state[3] >> 3) + (_state[4] >> 2)) >> 30;
164 _state[2] = _state[3];
165 _state[3] = t;
166 return (_state[0] + _state[1] + _state[2]);
167 }
168
169 /*---------------------------------------------------------------------------*/
170 /*---------------------------------------------------------------------------*/
171
177 bool validation(UIntType x) const { return val == x; }
178
179 /*---------------------------------------------------------------------------*/
180 /*---------------------------------------------------------------------------*/
181
187 bool operator==(const TKiss& rhs) const
188 {
189 return (_state[0] == rhs._state[0]) && (_state[1] == rhs._state[1]) && (_state[2] == rhs._state[2]) && (_state[3] == rhs._state[3]) && (_state[4] == rhs._state[4]);
190 }
191
192 private:
193
194 state_type _state[5];
195};
196
197/*---------------------------------------------------------------------------*/
198/*---------------------------------------------------------------------------*/
199
200typedef TKiss<UInt32, 0> Kiss;
201
202/*---------------------------------------------------------------------------*/
203/*---------------------------------------------------------------------------*/
204
205} // namespace Arcane::random
206
207/*---------------------------------------------------------------------------*/
208/*---------------------------------------------------------------------------*/
209
210#endif
result_type min() const
Returns the minimum possible value of a sequence.
Definition TKiss.h:57
void seed(UIntType x0)
Initialization of the seed array from the value x0. The seed array of this generator consists of five...
Definition TKiss.h:112
UIntType getState(Integer i) const
Method that returns the i-th component of the generator state. The complete generator state is given ...
Definition TKiss.h:144
void seed(UIntType x0, UIntType y0, UIntType z0, UIntType w0, UIntType carry0)
Initialization of the seed array from the argument values.
Definition TKiss.h:127
UIntType operator()()
Overdefinition of the () operator which returns the pseudo-random value. The generator state is modif...
Definition TKiss.h:154
void seed(UIntType *state)
Initialization of the seed array from the state. The generator state state must consist of five eleme...
Definition TKiss.h:95
TKiss(UIntType x0=30903, UIntType y0=30903, UIntType z0=30903, UIntType w0=30903, UIntType carry0=0)
Constructor with initialization of the seed array from the argument values.
Definition TKiss.h:77
result_type max() const
Returns the maximum possible value of a sequence.
Definition TKiss.h:67
bool validation(UIntType x) const
Validation function (I don't know what it's for!).
Definition TKiss.h:177
bool operator==(const TKiss &rhs) const
Overdefinition of the == operator.
Definition TKiss.h:187
Int32 Integer
Type representing an integer.