File size: 3,429 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
112
113
114
/// ------------------------------------------------------
/// 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>
    /// Wrapper for the .NET built-in PRNG. Not thread-safe by
    /// default.
    /// </summary>
    /// <remarks>
    /// Since the .NET implementation may change we cannot
    /// override and make a more efficient implementation of
    /// Byte() that would use bit-manipulation, because it
    /// would assume things about the implementation that might
    /// change.
    /// </remarks>
    public class RanSystem : Random
    {
        #region Constructors.
        /// <summary>
        /// Constructs the PRNG-object and seeds the PRNG with the current time of day.
        /// This is what you will mostly want to use.
        /// </summary>
        public RanSystem()

            : base()
        {
            Rand = new System.Random();
        }

        /// <summary>
        /// Constructs the PRNG-object using the designated seed.
        /// This is useful if you want to repeat experiments with the
        /// same sequence of pseudo-random numbers.
        /// </summary>
        public RanSystem(int seed)

            : base()
        {
            Rand = new System.Random(seed);
        }
        #endregion

        #region Internal variables.
        /// <summary>
        /// The .NET PRNG-object.
        /// </summary>
        System.Random Rand;
        #endregion

        #region Base-class overrides.
        /// <summary>
        /// Name of the RNG.
        /// </summary>
        public override string Name
        {
            get { return "RanSystem"; }
        }

        /// <summary>
        /// Draw a uniform random number in the exclusive range (0,1)
        /// </summary>
        /// <returns></returns>
        public sealed override double Uniform()
        {
            return Rand.NextDouble();
        }

        /// <summary>
        /// Draw a random boolean with equal probability of drawing true or false.
        /// </summary>
        /// <returns></returns>
        public sealed override bool Bool()
        {
            return Rand.Next(2) == 0;
        }

        /// <summary>
        /// Draw a random index from the range {0, .., n-1}
        /// </summary>
        /// <param name="n">The exclusive upper bound.</param>
        /// <returns></returns>
        public sealed override int Index(int n)
        {
            return Rand.Next(n);
        }

        /// <summary>
        /// Draw a random and uniform byte.
        /// </summary>
        public sealed override byte Byte()
        {
            // Re-use the Index() method.
            return Bytes(1)[0];
        }

        /// <summary>
        /// Draw an array of random and uniform bytes.
        /// </summary>
        /// <param name="length">The array length requested.</param>
        public sealed override byte[] Bytes(int length)
        {
            byte[] arr = new byte[length];

            Rand.NextBytes(arr);

            return arr;
        }
        #endregion
    }
}