Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
NormalDistribution.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/* NormalDistribution.h (C) 2000-2025 */
9/* */
10/* Randomize 'NormalDistribution'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_CORE_RANDOM_NORMALDISTRIBUTION_H
14#define ARCANE_CORE_RANDOM_NORMALDISTRIBUTION_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/FatalErrorException.h"
19#include "arcane/utils/Math.h"
20
21#include "arcane/core/random/RandomGlobal.h"
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane::random
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32/*
33 * Copyright Jens Maurer 2000-2001
34 * Permission to use, copy, modify, sell, and distribute this software
35 * is hereby granted without fee provided that the above copyright notice
36 * appears in all copies and that both that copyright notice and this
37 * permission notice appear in supporting documentation,
38 *
39 * Jens Maurer makes no representations about the suitability of this
40 * software for any purpose. It is provided "as is" without express or
41 * implied warranty.
42 *
43 * See http://www.boost.org for most recent version including documentation.
44 *
45 * $Id: NormalDistribution.h 3932 2004-08-23 08:45:03Z grospelx $
46 *
47 * Revision history
48 * 2001-02-18 moved to individual header files
49 */
50
51/*---------------------------------------------------------------------------*/
52/*---------------------------------------------------------------------------*/
53
54// deterministic polar method, uses trigonometric functions
55template <class UniformRandomNumberGenerator>
56class NormalDistribution
57{
58 public:
59
60 typedef UniformRandomNumberGenerator base_type;
61
62 explicit NormalDistribution(base_type& rng, Real mean = 0.0, Real sigma = 1.0)
63 : _rng(rng)
64 , _mean(mean)
65 , _sigma(sigma)
66 , _valid(false)
67 {
68 }
69
70 // compiler-generated copy constructor is NOT fine, need to purge cache
71 NormalDistribution(const NormalDistribution& other)
72 : _rng(other._rng)
73 , _mean(other._mean)
74 , _sigma(other._sigma)
75 , _valid(false)
76 {
77 }
78 // uniform_01 cannot be assigned, neither can this class
79
80 Real operator()()
81 {
82 if (!_valid) {
83 _r1 = _rng();
84 _r2 = _rng();
85 _cached_rho = math::sqrt(-2 * math::log(1.0 - _r2));
86 _valid = true;
87 }
88 else
89 _valid = false;
90 // Can we have a boost::mathconst please?
91 const double pi = 3.14159265358979323846;
92
93 return _cached_rho * (_valid ? cos(2 * pi * _r1) : sin(2 * pi * _r1)) * _sigma + _mean;
94 }
95
96 private:
97
99 const Real _mean, _sigma;
100 Real _r1 = 0.0;
101 Real _r2 = 0.0;
102 Real _cached_rho = 0.0;
103 bool _valid;
104};
105
106/*---------------------------------------------------------------------------*/
107/*---------------------------------------------------------------------------*/
108
109} // End namespace Arcane::random
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
114#endif
Generates a random number in the interval [0,1[.
Definition Uniform01.h:62
__host__ __device__ double sqrt(double v)
Square root of v.
Definition Math.h:142
__host__ __device__ double log(double v)
Natural logarithm of v.
Definition Math.h:42
double Real
Type representing a real number.