Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
UniformInt.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/* UniformInt.h (C) 2000-2004 */
9/* */
10/* Randomiser 'UniformInt'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_RANDOM_UNIFORMONINT_H
14#define ARCANE_RANDOM_UNIFORMONINT_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/FatalErrorException.h"
19
20#include "arcane/random/UniformSmallInt.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25ARCANE_BEGIN_NAMESPACE
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30RANDOM_BEGIN_NAMESPACE
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35/*
36 * Copyright Jens Maurer 2000-2001
37 * Permission to use, copy, modify, sell, and distribute this software
38 * is hereby granted without fee provided that the above copyright notice
39 * appears in all copies and that both that copyright notice and this
40 * permission notice appear in supporting documentation,
41 *
42 * Jens Maurer makes no representations about the suitability of this
43 * software for any purpose. It is provided "as is" without express or
44 * implied warranty.
45 *
46 * See http://www.boost.org for most recent version including documentation.
47 *
48 * $Id: UniformInt.h 3932 2004-08-23 08:45:03Z grospelx $
49 *
50 * Revision history
51 * 2001-02-18 moved to individual header files
52 */
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
56
57// uniform integer distribution on [min, max]
58template<class UniformRandomNumberGenerator,class IntType = int>
60{
61public:
63 typedef IntType result_type;
64 static const bool has_fixed_range = false;
65
66 UniformInt(base_type & rng, IntType min, IntType max)
67 : _rng(rng), _min(min), _max(max), _range(_max - _min),
68 _bmin(_rng.min()), _brange(_rng.max() - _bmin)
69 {
70 if(utils::equal_signed_unsigned(_brange, _range))
71 _range_comparison = 0;
72 else if(utils::lessthan_signed_unsigned(_brange, _range))
73 _range_comparison = -1;
74 else
75 _range_comparison = 1;
76 }
77 result_type operator()();
78 result_type min() const { return _min; }
79 result_type max() const { return _max; }
80 private:
81 typedef typename base_type::result_type base_result;
82 base_type & _rng;
83 result_type _min, _max, _range;
84 base_result _bmin, _brange;
85 int _range_comparison;
86};
87
88template<class UniformRandomNumberGenerator, class IntType>
90{
91 if(_range_comparison == 0) {
92 // this will probably never happen in real life
93 // basically nothing to do; just take care we don't overflow / underflow
94 return static_cast<result_type>(_rng() - _bmin) + _min;
95 } else if(_range_comparison < 0) {
96 // use rejection method to handle things like 0..3 --> 0..4
97 for(;;) {
98 // concatenate several invocations of the base RNG
99 // take extra care to avoid overflows
100 result_type limit;
101 if(_range == std::numeric_limits<result_type>::max()) {
102 limit = _range/(static_cast<result_type>(_brange)+1);
103 if(_range % static_cast<result_type>(_brange)+1 == static_cast<result_type>(_brange))
104 ++limit;
105 } else {
106 limit = (_range+1)/(static_cast<result_type>(_brange)+1);
107 }
108 // we consider "result" as expressed to base (_brange+1)
109 // for every power of (_brange+1), we determine a random factor
110 result_type result = 0;
111 result_type mult = 1;
112 while(mult <= limit) {
113 result += (_rng() - _bmin) * mult;
114 mult *= static_cast<result_type>(_brange)+1;
115 }
116 if(mult == limit)
117 // _range+1 is an integer power of _brange+1: no rejections required
118 return result;
119 // _range/mult < _brange+1 -> no endless loop
120 result += UniformInt<base_type,result_type>(_rng, 0, _range/mult)() * mult;
121 if(result <= _range)
122 return result + _min;
123 }
124 } else { // brange > range
125 if(_brange / _range > 4 /* quantization_cutoff */ ) {
126 // the new range is vastly smaller than the source range,
127 // so quantization effects are not relevant
128 return UniformSmallInt<base_type,result_type>(_rng, _min, _max)();
129 } else {
130 // use rejection method to handle things like 0..5 -> 0..4
131 for(;;) {
132 base_result result = _rng() - _bmin;
133 // result and range are non-negative, and result is possibly larger
134 // than range, so the cast is safe
135 if(result <= static_cast<base_result>(_range))
136 return result + _min;
137 }
138 }
139 }
140}
141
142/*---------------------------------------------------------------------------*/
143/*---------------------------------------------------------------------------*/
144
145RANDOM_END_NAMESPACE
146ARCANE_END_NAMESPACE
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
150
151#endif
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
void mult(ArrayView< T > lhs, ConstArrayView< T > copy_array)
Multiplie terme à terme les éléments de l'instance par les éléments du tableau copy_array.
Definition MathUtils.h:963