Arcane  v3.16.8.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-2025 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-2025 */
9/* */
10/* Randomiser 'UniformSmallInt'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_CORE_RANDOM_UNIFORMONSMALLINT_H
14#define ARCANE_CORE_RANDOM_UNIFORMONSMALLINT_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/FatalErrorException.h"
19
20#include "arcane/core/random/NormalDistribution.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25ARCANE_BEGIN_NAMESPACE
26RANDOM_BEGIN_NAMESPACE
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31/*
32 * Copyright Jens Maurer 2000-2001
33 * Permission to use, copy, modify, sell, and distribute this software
34 * is hereby granted without fee provided that the above copyright notice
35 * appears in all copies and that both that copyright notice and this
36 * permission notice appear in supporting documentation,
37 *
38 * Jens Maurer makes no representations about the suitability of this
39 * software for any purpose. It is provided "as is" without express or
40 * implied warranty.
41 *
42 * See http://www.boost.org for most recent version including documentation.
43 *
44 * $Id: UniformSmallInt.h 3932 2004-08-23 08:45:03Z grospelx $
45 *
46 * Revision history
47 * 2001-02-18 moved to individual header files
48 */
49
50/*---------------------------------------------------------------------------*/
51/*---------------------------------------------------------------------------*/
52
53// uniform integer distribution on a small range [min, max]
54template<class UniformRandomNumberGenerator, class IntType = int>
55class UniformSmallInt
56{
57public:
58 typedef UniformRandomNumberGenerator base_type;
59 typedef IntType result_type;
60 static const bool has_fixed_range = false;
61
62 UniformSmallInt(base_type & rng, IntType min, IntType max);
63 result_type operator()()
64 {
65 // we must not use the low bits here, because LCGs get very bad then
66 return ((_rng() - _rng.min()) / _factor) % _range + _min;
67 }
68 result_type min() const { return _min; }
69 result_type max() const { return _max; }
70private:
71 typedef typename base_type::result_type base_result;
72 base_type & _rng;
73 IntType _min, _max;
74 base_result _range;
75 int _factor;
76};
77
78template<class UniformRandomNumberGenerator, class IntType>
80UniformSmallInt(base_type & rng, IntType min, IntType max)
81 : _rng(rng), _min(min), _max(max),
82 _range(static_cast<base_result>(_max-_min)+1), _factor(1)
83{
84 // LCGs get bad when only taking the low bits.
85 // (probably put this logic into a partial template specialization)
86 // Check how many low bits we can ignore before we get too much
87 // quantization error.
88 base_result r_base = _rng.max() - _rng.min();
89 if(r_base == std::numeric_limits<base_result>::max()) {
90 _factor = 2;
91 r_base /= 2;
92 }
93 r_base += 1;
94 if(r_base % _range == 0) {
95 // No quantization effects, good
96 _factor = r_base / _range;
97 } else {
98 // carefully avoid overflow; pessimizing heree
99 for( ; r_base/_range/32 >= _range; _factor *= 2)
100 r_base /= 2;
101 }
102}
103
104/*---------------------------------------------------------------------------*/
105/*---------------------------------------------------------------------------*/
106
107RANDOM_END_NAMESPACE
108ARCANE_END_NAMESPACE
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/
112
113#endif