File size: 6,067 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/// ------------------------------------------------------
/// 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) based on the Ran2 algorithm from the book:
    /// 'Numerical Recipes in C' chapter 7.1 and which is originally
    /// due to L'Ecuyer with Bays-Durham shuffle and added safeguards.
    /// Period is greater than 2 * 10^18. Not thread-safe.
    /// </summary>
    /// <remarks>
    /// We MUST use division when generating random integers in a certain range.
    /// Do NOT use bit-manipulation because the low-order bits of are not that random!
    /// This works by default because Uniform() is used for creating integers through
    /// the implementation of the Index() methods in the Random base-class.
    /// </remarks>
    public class Ran2 : RanInt32
    {
        #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 Ran2()

            : base()
        {
            Seed();
        }

        /// <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 Ran2(Int32 seed)

            : base()
        {
            Seed(seed);
        }
        #endregion

        #region Iterator-class (internal use only).
        class Iterator
        {
            Int32 IM, IA, IQ, IR;

            public Int32 Idum
            {
                get;
                private set;
            }

            public Iterator(Int32 im, Int32 ia, Int32 iq, Int32 ir)
            {
                IM = im;
                IA = ia;
                IQ = iq;
                IR = ir;
            }

            public void Seed(Int32 seed)
            {
                Idum = seed;
            }

            public Int32 DoRand()
            {
                Int32 k;

                k = Idum / IQ;

                Idum = IA * (Idum - k * IQ) - IR * k;

                if (Idum < 0)
                {
                    Idum += IM;
                }

                return Idum;
            }
        }
        #endregion

        #region Internal definitions and variables.
        static readonly Int32 IM0 = 2147483563;
        static readonly Int32 IM1 = 2147483399;
        static readonly Int32 IA0 = 40014;
        static readonly Int32 IA1 = 40692;
        static readonly Int32 IQ0 = 53668;
        static readonly Int32 IQ1 = 52774;
        static readonly Int32 IR0 = 12211;
        static readonly Int32 IR1 = 3791;
        static readonly Int32 NTAB = 32;
        static readonly Int32 IMM = IM0 - 1;
        static readonly Int32 NDIV = 1 + IMM / NTAB;
        static readonly Int32 WARMUP = 1024 + 8;
        static readonly Int32 WARMUP2 = 200;

        Iterator[] Iterators = { new Iterator(IM0, IA0, IQ0, IR0), new Iterator(IM1, IA1, IQ1, IR1) };

        Int32 iy = 0;
        Int32[] iv = new Int32[NTAB];

        /// <summary>
        /// Is PRNG ready for use?
        /// </summary>
        bool IsReady = false;
        #endregion

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

            Iterators[0].DoRand();
            Iterators[1].DoRand();

            {
                // Will be in the range 0..NTAB-1.
                int j = iy / NDIV;

                Debug.Assert(j >= 0 && j < NTAB);

                // Idum is shuffled, idum0 and idum1 are
                // combined to generate output.
                iy = iv[j] - Iterators[1].Idum;
                iv[j] = Iterators[0].Idum;

                if (iy < 1)
                {
                    iy += IMM;
                }
            }

            Debug.Assert(iy >= 0 && iy <= RandMax);

            return iy;
        }

        /// <summary>
        /// The maximum possible value returned by Rand().
        /// </summary>
        public sealed override Int32 RandMax
        {
            get { return IM0 - 1; }
        }

        /// <summary>
        /// Seed with an integer.
        /// </summary>
        protected sealed override void Seed(Int32 seed)
        {
            int j;

            // Ensure seed>0
            if (seed == 0)
            {
                seed = 1;
            }
            else if (seed < 0)
            {
                seed = -seed;
            }

            Iterators[0].Seed(seed);
            Iterators[1].Seed(seed);

            // Perform initial warm-ups.
            for (j = 0; j < WARMUP; j++)
            {
                Iterators[0].DoRand();
            }

            for (j = NTAB - 1; j >= 0; j--)
            {
                iv[j] = Iterators[0].DoRand();
            }

            iy = iv[0];

            // PRNG is now ready for use.
            IsReady = true;

            // Perform additional warm-ups.
            for (j = 0; j < WARMUP2; j++)
            {
                Rand();
            }
        }
        #endregion

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