File size: 3,728 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
115
116
117
118
119
120
121
122
123
124
125
126
/// ------------------------------------------------------
/// 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
{
    /// <summary>
    /// Pseudo-Random Number Generator (PRNG) base-class for a generator of UInt32 integers.
    /// </summary>
    /// <remarks>
    /// It is somewhat tricky to implement Index() using integer-operations and get the
    /// rounding right for all cases, so we reuse the base-class implementation which
    /// indirectly uses Uniform().
    /// </remarks>
    public abstract class RanInt32 : Random
    {
        #region Constructors.
        /// <summary>
        /// Constructs the PRNG-object.
        /// </summary>
        public RanInt32()

            : base()
        {
            _randMaxHalf = RandMax / 2;
            _randInv = 1.0/((double)RandMax + 2);
        }
        #endregion

        #region Internal variables.
        /// <summary>
        /// Used in Bool(), for convenience and speed.
        /// </summary>
        Int32 _randMaxHalf;

        /// <summary>
        /// Used in Uniform(), for convenience and speed.
        /// </summary>
        double _randInv;
        #endregion

        #region PRNG Implementation.
        /// <summary>
        /// Draw a random number in inclusive range {0, .., RandMax}
        /// </summary>
        public abstract Int32 Rand();

        /// <summary>
        /// The maximum possible value returned by Rand().
        /// </summary>
        public abstract Int32 RandMax
        {
            get;
        }

        /// <summary>
        /// Seed with the time of day.
        /// </summary>
        protected void Seed()
        {
            Int32 seed = (Int32)(DateTime.Now.Ticks % (long)RandMax);

            Seed(seed);
        }

        /// <summary>
        /// Seed with an integer.
        /// </summary>
        protected abstract void Seed(Int32 seed);
        #endregion

        #region Base-class overrides.
        /// <summary>
        /// Draw a uniform random number in the exclusive range (0,1)
        /// Thread-safe if Rand() is thread-safe.
        /// </summary>
        /// <remarks>
        /// Assumes that Rand() is in {0, .., RandMax}.
        /// </remarks>
        public override double Uniform()
        {
            double rand = (double)Rand() + 1;
            double value = rand * _randInv;

            Debug.Assert(value > 0 && value < 1);

            return value;
        }

        /// <summary>
        /// Draw a random boolean with equal probability of true or false.
        /// Thread-safe if Rand() is thread-safe.
        /// </summary>
        public override bool Bool()
        {
            return Rand() < _randMaxHalf;
        }

        /// <summary>
        /// Draw a random and uniform byte.
        /// Thread-safe if Rand() is thread-safe.
        /// </summary>
        /// <remarks>
        /// The least significant bits are not that statistically random,
        /// hence we must use the most significant bits by a bit-shift.
        /// </remarks>
        public override byte Byte()
        {
            Int32 r = Rand();
            Int32 value = r >> 23;

            Debug.Assert(value >= 0 && value <= System.Byte.MaxValue);

            byte b = (byte)value;

            return b;
        }
        #endregion
    }
}