ningshuxia
2022-12-12 4f1314cb69a47c22e52f1efdc4b4be6c1d55143d
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// <copyright file="Mrg32k3a.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2014 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
 
using System.Collections.Generic;
using System.Runtime.Serialization;
 
#if !NETSTANDARD1_3
using System;
using System.Runtime;
#endif
 
namespace IStation.Numerics.Random
{
    /// <summary>
    /// A 32-bit combined multiple recursive generator with 2 components of order 3.
    /// </summary>
    /// <remarks>Based off of P. L'Ecuyer, "Combined Multiple Recursive Random Number Generators," Operations Research, 44, 5 (1996), 816--822. </remarks>
    ///
    [Serializable]
    [DataContract(Namespace = "urn:IStation/Numerics/Random")]
    public class Mrg32k3a : RandomSource
    {
        const double A12 = 1403580;
        const double A13 = 810728;
        const double A21 = 527612;
        const double A23 = 1370589;
        const double Modulus1 = 4294967087;
        const double Modulus2 = 4294944443;
 
        const double Reciprocal = 1.0/Modulus1;
 
        [DataMember(Order = 1)]
        double _xn1 = 1;
        [DataMember(Order = 2)]
        double _xn2 = 1;
        [DataMember(Order = 3)]
        double _xn3;
        [DataMember(Order = 4)]
        double _yn1 = 1;
        [DataMember(Order = 5)]
        double _yn2 = 1;
        [DataMember(Order = 6)]
        double _yn3 = 1;
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Mcg31m1"/> class using
        /// a seed based on time and unique GUIDs.
        /// </summary>
        /// <remarks>If the seed value is zero, it is set to one. Uses the
        /// value of <see cref="Control.ThreadSafeRandomNumberGenerators"/> to
        /// set whether the instance is thread safe.</remarks>
        public Mrg32k3a() : this(RandomSeed.Robust())
        {
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Mcg31m1"/> class using
        /// a seed based on time and unique GUIDs.
        /// </summary>
        /// <param name="threadSafe">if set to <c>true</c> , the class is thread safe.</param>
        public Mrg32k3a(bool threadSafe) : this(RandomSeed.Robust(), threadSafe)
        {
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Mrg32k3a"/> class.
        /// </summary>
        /// <param name="seed">The seed value.</param>
        /// <remarks>If the seed value is zero, it is set to one. Uses the
        /// value of <see cref="Control.ThreadSafeRandomNumberGenerators"/> to
        /// set whether the instance is thread safe.</remarks>
        public Mrg32k3a(int seed)
        {
            if (seed == 0)
            {
                seed = 1;
            }
 
            _xn3 = (uint)seed;
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Mrg32k3a"/> class.
        /// </summary>
        /// <param name="seed">The seed value.</param>
        /// <param name="threadSafe">if set to <c>true</c>, the class is thread safe.</param>
        public Mrg32k3a(int seed, bool threadSafe) : base(threadSafe)
        {
            if (seed == 0)
            {
                seed = 1;
            }
 
            _xn3 = (uint)seed;
        }
 
        /// <summary>
        /// Returns a random double-precision floating point number greater than or equal to 0.0, and less than 1.0.
        /// </summary>
        protected sealed override double DoSample()
        {
            double xn = A12*_xn2 - A13*_xn3;
            double k = (long)(xn/Modulus1);
            xn -= k*Modulus1;
            if (xn < 0)
            {
                xn += Modulus1;
            }
 
            double yn = A21*_yn1 - A23*_yn3;
            k = (long)(yn/Modulus2);
            yn -= k*Modulus2;
            if (yn < 0)
            {
                yn += Modulus2;
            }
 
            _xn3 = _xn2;
            _xn2 = _xn1;
            _xn1 = xn;
            _yn3 = _yn2;
            _yn2 = _yn1;
            _yn1 = yn;
 
            if (xn <= yn)
            {
                return (xn - yn + Modulus1)*Reciprocal;
            }
 
            return (xn - yn)*Reciprocal;
        }
 
        /// <summary>
        /// Fills an array with random numbers greater than or equal to 0.0 and less than 1.0.
        /// </summary>
        /// <remarks>Supports being called in parallel from multiple threads.</remarks>
        public static void Doubles(double[] values, int seed)
        {
            double x1 = 1;
            double x2 = 1;
            double x3 = (uint)seed;
            double y1 = 1;
            double y2 = 1;
            double y3 = 1;
 
            for (int i = 0; i < values.Length; i++)
            {
                double xn = A12*x2 - A13*x3;
                double k = (long)(xn/Modulus1);
                xn -= k*Modulus1;
                if (xn < 0)
                {
                    xn += Modulus1;
                }
 
                double yn = A21*y1 - A23*y3;
                k = (long)(yn/Modulus2);
                yn -= k*Modulus2;
                if (yn < 0)
                {
                    yn += Modulus2;
                }
 
                x3 = x2;
                x2 = x1;
                x1 = xn;
                y3 = y2;
                y2 = y1;
                y1 = yn;
 
                values[i] = xn <= yn ? (xn - yn + Modulus1)*Reciprocal : (xn - yn)*Reciprocal;
            }
        }
 
        /// <summary>
        /// Returns an array of random numbers greater than or equal to 0.0 and less than 1.0.
        /// </summary>
        /// <remarks>Supports being called in parallel from multiple threads.</remarks>
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public static double[] Doubles(int length, int seed)
        {
            var data = new double[length];
            Doubles(data, seed);
            return data;
        }
 
        /// <summary>
        /// Returns an infinite sequence of random numbers greater than or equal to 0.0 and less than 1.0.
        /// </summary>
        /// <remarks>Supports being called in parallel from multiple threads, but the result must be enumerated from a single thread each.</remarks>
        public static IEnumerable<double> DoubleSequence(int seed)
        {
            double x1 = 1;
            double x2 = 1;
            double x3 = (uint)seed;
            double y1 = 1;
            double y2 = 1;
            double y3 = 1;
 
            while (true)
            {
                double xn = A12*x2 - A13*x3;
                double k = (long)(xn/Modulus1);
                xn -= k*Modulus1;
                if (xn < 0)
                {
                    xn += Modulus1;
                }
 
                double yn = A21*y1 - A23*y3;
                k = (long)(yn/Modulus2);
                yn -= k*Modulus2;
                if (yn < 0)
                {
                    yn += Modulus2;
                }
 
                x3 = x2;
                x2 = x1;
                x1 = xn;
                y3 = y2;
                y2 = y1;
                y1 = yn;
 
                yield return xn <= yn ? (xn - yn + Modulus1)*Reciprocal : (xn - yn)*Reciprocal;
            }
        }
    }
}