File size: 3,244 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
/// ------------------------------------------------------
/// 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
{
    /// <remarks>
    /// Various methods for creating random integers.
    /// </remarks>
    public abstract partial class Random
    {
        /// <summary>
        /// Draw a random integer from the set {0, .., n-1} with uniform probability.
        /// Thread-safe if Uniform() is thread-safe.
        /// </summary>
        /// <remarks>
        /// The default implementation uses Uniform() to generate an integer.
        /// This is because many PRNGs must use division instead of modulo
        /// arithmetics due to the lower-order bits having little randomness.
        /// Assume Uniform cannot generate an exact value of one,
        /// otherwise this must have been taken into account to ensure
        /// uniform probability of choosing the different indices. 
        /// </remarks>
        public virtual int Index(int n)
        {
            Debug.Assert(n >= 1);

            double indexD = Uniform(0, n);

            Debug.Assert(indexD > 0 && indexD < n);

            int index = (int)Math.Truncate(indexD);

            Debug.Assert(index >= 0 && index <= n - 1);

            return index;
        }

        /// <summary>
        /// Draw two distinct integers from the set {0, .., n-1} with equal probability.
        /// Thread-safe if Uniform() is thread-safe.
        /// </summary>
        /// <param name="index1">Reference to the first integer drawn.</param>
        /// <param name="index2">Reference to the second integer drawn.</param>
        public virtual void Index2(int n, out int index1, out int index2)
        {
            Debug.Assert(n >= 2);

            int r1, r2;

            r1 = Index(n);
            r2 = Index(n - 1);

            int i1 = r1;
            int i2 = (r1 + r2 + 1) % n;

            Debug.Assert(i1 != i2);

            index1 = i1;
            index2 = i2;
        }

        /// <summary>
        /// Draw a random integer from the set {0, .., n-1} according
        /// to the supplied probability distribution. Probabilities are
        /// assumed to sum to one. Time-complexity is O(n),
        /// where n is the length of the probabilities-array.
        /// Use this function if you only need to draw a few random numbers,
        /// otherwise create an IndexDistribution-object.
        /// Thread-safe if Uniform() is thread-safe.
        /// </summary>
        /// <param name="probabilities">Probability distribution.</param>
        public int Index(double[] probabilities)
        {
            double sum = 0;
            double r = Uniform();

            int i;

            for (i = 0; sum < r && i < probabilities.Length; i++)
            {
                sum += probabilities[i];
            }

            return i - 1;
        }
    }
}