File size: 2,291 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/// ------------------------------------------------------
/// RandomOps - (Pseudo) Random Number Generator For C#
/// Copyright (C) 2003-2010 Magnus Erik Hvass Pedersen.
/// Please see the file license.txt for license details.
/// RandomOps on the internet: http://www.Hvass-Labs.org/
/// ------------------------------------------------------

using System;

namespace RandomOps
{
    /// <remarks>
    /// Implements a Gaussian RNG using the Uniform() method.
    /// </remarks>
    public abstract partial class Random
    {
        /// <summary>
        /// Next Gaussian random number.
        /// </summary>
        double _gaussian;

        /// <summary>
        /// Does _gaussian hold a value?
        /// </summary>
        bool _gaussReady = false;

        /// <summary>
        /// Draw a Gaussian (or normally) distributed random number, with designated
        /// mean and deviation. Thread-safe if Gauss() is thread-safe.
        /// </summary>
        /// <param name="mean">The mean of the distribution, e.g. 0.</param>
        /// <param name="deviation">The deviation of the distribution, e.g. 1.</param>
        public virtual double Gauss(double mean, double deviation)
        {
            return deviation * Gauss() + mean;
        }

        /// <summary>
        /// Draw a Gaussian (or normally) distributed random number, with mean 0 and
        /// deviation 1. Not thread-safe.
        /// </summary>
        public virtual double Gauss()
        {
            double value;

            if (_gaussReady)
            {
                value = _gaussian;
                _gaussReady = false;
            }
            else
            {
                double v1, v2, rsq;

                // Pick two uniform numbers in the unit-radius 2-dim ball.
                Disk(out v1, out v2, out rsq);

                double fac = Math.Sqrt(-2.0 * Math.Log(rsq) / rsq);

                // Now make the Box-Muller transformation to get two normal deviates.
                // Return one and save the other for next time.
                _gaussian = v1 * fac;
                _gaussReady = true;

                value = v2 * fac;
            }

            return value;
        }
    }
}