13#ifndef ARCANE_CORE_RANDOM_UNIFORMONINT_H
14#define ARCANE_CORE_RANDOM_UNIFORMONINT_H
18#include "arcane/utils/FatalErrorException.h"
20#include "arcane/core/random/UniformSmallInt.h"
25namespace Arcane::random
54template <
class UniformRandomNumberGenerator,
class IntType =
int>
59 typedef UniformRandomNumberGenerator base_type;
60 typedef IntType result_type;
61 static const bool has_fixed_range =
false;
63 UniformInt(base_type& rng, IntType min, IntType max)
69 , _brange(_rng.max() - _bmin)
71 if (utils::equal_signed_unsigned(_brange, _range))
72 _range_comparison = 0;
73 else if (utils::lessthan_signed_unsigned(_brange, _range))
74 _range_comparison = -1;
76 _range_comparison = 1;
78 result_type operator()();
79 result_type min()
const {
return _min; }
80 result_type max()
const {
return _max; }
84 typedef typename base_type::result_type base_result;
86 result_type _min, _max, _range;
87 base_result _bmin, _brange;
88 int _range_comparison;
91template <
class UniformRandomNumberGenerator,
class IntType>
92inline IntType UniformInt<UniformRandomNumberGenerator, IntType>::operator()()
94 if (_range_comparison == 0) {
97 return static_cast<result_type
>(_rng() - _bmin) + _min;
99 else if (_range_comparison < 0) {
105 if (_range == std::numeric_limits<result_type>::max()) {
106 limit = _range / (
static_cast<result_type
>(_brange) + 1);
107 if (_range %
static_cast<result_type
>(_brange) + 1 ==
static_cast<result_type
>(_brange))
111 limit = (_range + 1) / (
static_cast<result_type
>(_brange) + 1);
115 result_type result = 0;
116 result_type
mult = 1;
117 while (mult <= limit) {
118 result += (_rng() - _bmin) * mult;
119 mult *=
static_cast<result_type
>(_brange) + 1;
126 if (result <= _range)
127 return result + _min;
131 if (_brange / _range > 4 ) {
139 base_result result = _rng() - _bmin;
142 if (result <=
static_cast<base_result
>(_range))
143 return result + _min;
void mult(ArrayView< T > lhs, ConstArrayView< T > copy_array)
Multiplies the elements of the instance term-by-term by the elements of the array copy_array.