Arcane  v3.16.8.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
Uniform01.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/* Uniform01.h (C) 2000-2025 */
9/* */
10/* Randomiser '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 typedef UniformRandomNumberGenerator base_type;
65 typedef Real result_type;
66 static const bool has_fixed_range = false;
67
68 explicit Uniform01(base_type & rng)
69 : _rng(rng) { }
70 // compiler-generated copy ctor is fine
71 // assignment is disallowed because there is a reference member
72
75 {
76 return apply(_rng);
77 }
78
83 static Real apply(base_type& _rng)
84 {
85 // Comme _rng() peut retourner 1.0, fait une boucle
86 // permettant de relancer le générateur tant que
87 // la valeur retournée vaut 1.0. Pour éviter
88 // une boucle infinie, effectue au maximum 100 itérations
89 // (la probabilité de tirer 100 fois la valeur 1.0
90 // devrait être quasiment nulle).
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
108 static ARCANE_DEPRECATED_122 Real apply(const base_type& _rng,typename base_type::result_type rng_val)
109 {
110 Real r = _apply(_rng,rng_val);
111 if (r<1.0)
112 return r;
113 // Retourne un nombre proche de 1 mais inférieur.
114 return (1.0-1.0e-10);
115 }
116
117 static Real min() { return 0.0; }
119 static Real max() { return 1.0; }
120
121 private:
122
123 static Real _apply(const base_type& _rng,typename base_type::result_type rng_val)
124 {
125 return static_cast<Real>(rng_val - _rng.min()) /
126 (static_cast<Real>(_rng.max()-_rng.min()) +
127 (std::numeric_limits<base_result>::is_integer ? 1.0 : 0.0));
128 }
129
130 private:
131
132 typedef typename base_type::result_type base_result;
133 base_type & _rng;
134};
135
136/*---------------------------------------------------------------------------*/
137/*---------------------------------------------------------------------------*/
138
139}
140
141/*---------------------------------------------------------------------------*/
142/*---------------------------------------------------------------------------*/
143
144#endif
static Real min()
Valeur minimal retournée.
Definition Uniform01.h:117
static Real max()
Borne supérieure (non atteinte) des valeurs retournées.
Definition Uniform01.h:119
static ARCANE_DEPRECATED_122 Real apply(const base_type &_rng, typename base_type::result_type rng_val)
Definition Uniform01.h:108
static Real apply(base_type &_rng)
Génère un nombre aléatoire compris dans l'intervalle [0,1[ à partir du générateur _rng.
Definition Uniform01.h:83
Real operator()()
Génère un nombre aléatoire compris dans l'intervalle [0,1[.
Definition Uniform01.h:74
double Real
Type représentant un réel.