Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
UniformSmallInt.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/* UniformSmallInt.h (C) 2000-2004 */
9/* */
10/* Randomiser 'UniformSmallInt'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_RANDOM_UNIFORMONSMALLINT_H
14#define ARCANE_RANDOM_UNIFORMONSMALLINT_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/FatalErrorException.h"
19
20#include "arcane/random/NormalDistribution.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: UniformSmallInt.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 a small range [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 UniformSmallInt(base_type & rng, IntType min, IntType max);
67 result_type operator()()
68 {
69 // we must not use the low bits here, because LCGs get very bad then
70 return ((_rng() - _rng.min()) / _factor) % _range + _min;
71 }
72 result_type min() const { return _min; }
73 result_type max() const { return _max; }
74private:
75 typedef typename base_type::result_type base_result;
76 base_type & _rng;
77 IntType _min, _max;
78 base_result _range;
79 int _factor;
80};
81
82template<class UniformRandomNumberGenerator, class IntType>
84UniformSmallInt(base_type & rng, IntType min, IntType max)
85 : _rng(rng), _min(min), _max(max),
86 _range(static_cast<base_result>(_max-_min)+1), _factor(1)
87{
88 // LCGs get bad when only taking the low bits.
89 // (probably put this logic into a partial template specialization)
90 // Check how many low bits we can ignore before we get too much
91 // quantization error.
92 base_result r_base = _rng.max() - _rng.min();
93 if(r_base == std::numeric_limits<base_result>::max()) {
94 _factor = 2;
95 r_base /= 2;
96 }
97 r_base += 1;
98 if(r_base % _range == 0) {
99 // No quantization effects, good
100 _factor = r_base / _range;
101 } else {
102 // carefully avoid overflow; pessimizing heree
103 for( ; r_base/_range/32 >= _range; _factor *= 2)
104 r_base /= 2;
105 }
106}
107
108/*---------------------------------------------------------------------------*/
109/*---------------------------------------------------------------------------*/
110
111RANDOM_END_NAMESPACE
112ARCANE_END_NAMESPACE
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
116
117#endif
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120