Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
Uniform01.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/* Uniform01.h (C) 2000-2025 */
9/* */
10/* Randomize 'Uniform01'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_CORE_RANDOM_UNIFORM01_H
14#define ARCANE_CORE_RANDOM_UNIFORM01_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/FatalErrorException.h"
19
20#include "arcane/core/random/RandomGlobal.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane::random
26{
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: Uniform01.h 6199 2005-11-08 13:52:46Z grospelx $
45 *
46 * Revision history
47 * 2001-02-18 moved to individual header files
48 */
49
50// Because it is so commonly used: uniform distribution on the real [0..1)
51// range. This allows for specializations to avoid a costly FP division
60template <class UniformRandomNumberGenerator>
61class Uniform01
62{
63 public:
64
65 typedef UniformRandomNumberGenerator base_type;
66 typedef Real result_type;
67 static const bool has_fixed_range = false;
68
69 explicit Uniform01(base_type& rng)
70 : _rng(rng)
71 {}
72 // compiler-generated copy ctor is fine
73 // assignment is disallowed because there is a reference member
74
77 {
78 return apply(_rng);
79 }
80
85 static Real apply(base_type& _rng)
86 {
87 // Since _rng() can return 1.0, it loops, restarting the generator
88 // as long as the returned value is 1.0. To avoid an infinite loop,
89 // it performs a maximum of 100 iterations (the probability of
90 // drawing the value 1.0 100 times should be almost zero).
91 Real rng_val = _apply(_rng, _rng());
92 for (int x = 0; x < 100; ++x) {
93 if (rng_val < 1.0)
94 return rng_val;
95 rng_val = _apply(_rng, _rng());
96 }
97 return 1.0 - 1.0e-10;
98 }
99
106 static ARCANE_DEPRECATED_122 Real apply(const base_type& _rng, typename base_type::result_type rng_val)
107 {
108 Real r = _apply(_rng, rng_val);
109 if (r < 1.0)
110 return r;
111 // Returns a number close to 1 but less than it.
112 return (1.0 - 1.0e-10);
113 }
114
115 static Real min() { return 0.0; }
117 static Real max() { return 1.0; }
118
119 private:
120
121 static Real _apply(const base_type& _rng, typename base_type::result_type rng_val)
122 {
123 return static_cast<Real>(rng_val - _rng.min()) /
124 (static_cast<Real>(_rng.max() - _rng.min()) +
125 (std::numeric_limits<base_result>::is_integer ? 1.0 : 0.0));
126 }
127
128 private:
129
130 typedef typename base_type::result_type base_result;
131 base_type& _rng;
132};
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
136
137} // namespace Arcane::random
138
139/*---------------------------------------------------------------------------*/
140/*---------------------------------------------------------------------------*/
141
142#endif
static Real min()
Minimum returned value.
Definition Uniform01.h:115
static Real max()
Upper bound (not reached) of the returned values.
Definition Uniform01.h:117
static ARCANE_DEPRECATED_122 Real apply(const base_type &_rng, typename base_type::result_type rng_val)
Definition Uniform01.h:106
static Real apply(base_type &_rng)
Generates a random number within the interval [0,1[ from the generator _rng.
Definition Uniform01.h:85
Real operator()()
Generates a random number within the interval [0,1[.
Definition Uniform01.h:76
double Real
Type representing a real number.