File size: 2,487 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
/// ------------------------------------------------------
/// 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;
using System.Diagnostics;

namespace RandomOps
{
    /// <remarks>
    /// Implements RNG of a disk.
    /// </remarks>
    public abstract partial class Random
    {
        /// <summary>
        /// Generate a uniform random point from the unit-radius 2-dimensional disk.
        /// Thread-safe if Uniform() is thread-safe.
        /// </summary>
        public virtual double[] Disk()
        {
            double[] x = new double[2];

            double sumSquares;

            Disk(out x[0], out x[1], out sumSquares);

            return x;
        }

        /// <summary>
        /// Generate a uniform random point from the unit-radius 2-dimensional disk.
        /// Thread-safe if Uniform() is thread-safe.
        /// </summary>
        /// <param name="x">Random point x</param>
        /// <param name="y">Random point y</param>
        /// <param name="sumSquares">Equals x*x + y*y</param>
        public virtual void Disk(out double x, out double y, out double sumSquares)
        {
            // Pick two uniform numbers in the square (-1,1) x (-1,1). See if the
            // numbers are inside the unit circle, and if they are not, try again.

            // Succesful points occupy the inside of the unit circle, whose area is
            // pi, or about 3.14. Since we sample uniformly from a square of size 4,
            // the probability of the loop succeeding in a single iteration is pi/4,
            // or about 0.7854. 
            // Probability of success in two iterations is therefore 0.954, in three
            // iterations it is about 0.990, and the probability of success in
            // four successive iterations is approximately 0.998, etc. -- provided there
            // is no correlation between calls to Uniform(-1, 1).

            // The average number of iterations before success is 1.27

            do
            {
                x = Uniform(-1, 1);
                y = Uniform(-1, 1);
                sumSquares = x * x + y * y;
            }
            while (sumSquares > 1);
        }
    }
}