File size: 3,927 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/// ------------------------------------------------------
/// 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/
/// ------------------------------------------------------

namespace RandomOps
{
    /// <summary>
    /// Base-class for an RNG.
    /// </summary>
    /// <remarks>
    /// You must at least implement the Uniform() method, which is
    /// used by default by all the other methods that can be overrided
    /// to provide more efficient implementations for your particular RNG.
    /// </remarks>
    public abstract partial class Random
    {
        /// <summary>
        /// Name of the RNG.
        /// </summary>
        public abstract string Name
        {
            get;
        }

        /// <summary>
        /// Draw a uniform random number in the exclusive range (0,1).
        /// </summary>
        /// <remarks>
        /// This must be implemented in every inherited class. It is important
        /// that it does not return the end-point values of zero or one, or
        /// else some of the methods which use Uniform() to generate their
        /// random numbers will not work.
        /// </remarks>
        public abstract double Uniform();

        /// <summary>
        /// Draw a uniform random number in the exclusive range (low,high).
        /// Thread-safe if Uniform() is thread-safe.
        /// </summary>
        /// <param name="low">The lowest value in the range.</param>
        /// <param name="high">The highest value in the range.</param>
        public virtual double Uniform(double low, double high)
        {
            // Re-use the paremeter-less Uniform() method.

            return Uniform() * (high - low) + low;
        }

        /// <summary>
        /// Draw a random boolean with equal probability of true or false.
        /// Thread-safe if Uniform() is thread-safe.
        /// </summary>
        public virtual bool Bool()
        {
            // Re-use the Uniform() method.
            return Uniform() < 0.5;
        }

        /// <summary>
        /// Draw a random boolean with probability p of true and (1-p) of false.
        /// Thread-safe if Uniform() is thread-safe.
        /// </summary>
        public virtual bool Bool(double p)
        {
            // Re-use the Uniform() method.
            return Uniform() < p;
        }

        /// <summary>
        /// Draw a random and uniform byte.
        /// Thread-safe if Index() is thread-safe.
        /// </summary>
        /// <remarks>
        /// The default implementation uses Index() which in turn uses
        /// Uniform(). This is quite expensive for generating just
        /// one byte, but is the most generally viable way of doing it
        /// for many PRNGs, because their low-order bits are not very
        /// random. Some PRNGs may offer much better ways of generating
        /// just a single random byte, in which case this method should
        /// of course be overrided.
        /// </remarks>
        public virtual byte Byte()
        {
            // Re-use the Index() method.
            return (byte)Index((int)System.Byte.MaxValue + 1);
        }

        /// <summary>
        /// Draw an array of random and uniform bytes.
        /// Thread-safe if Byte() is thread-safe.
        /// </summary>
        /// <param name="length">The array length requested.</param>
        public virtual byte[] Bytes(int length)
        {
             // Re-use the Byte() method.

            byte[] arr = new byte[length];

            for (int i = 0; i < length; i++)
            {
                arr[i] = Byte();
            }

            return arr;
        }
    }
}