File size: 3,879 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
/// ------------------------------------------------------
/// 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>
    /// A set of integers enumerated from zero and upwards that
    /// can be drawn at random according to the given probability
    /// distribution. Probabilities are assumed to sum to one.
    /// Various functions for drawing numbers are supplied with
    /// different time-complexity advantages. Thread-safe if
    /// supplied RNG's Uniform() is thread-safe.
    /// </summary>
    public class IndexDistribution
    {
        #region Constructors.
        /// <summary>
        /// Create the object. Time-complexity for doing this is O(n),
        /// where n is the length of the probabilities-array. 
        /// </summary>
        /// <param name="rand">RNG object to use.</param>
        /// <param name="probabilities">Probability distribution.</param>
        public IndexDistribution(Random rand, double[] probabilities)
        {
            Rand = rand;

            ProbabilitySum = new double[probabilities.Length + 1];
            ProbabilitySum[0] = 0;

            double sum = 0;
            for (int i = 0; i < probabilities.Length; i++)
            {
                sum += probabilities[i];
                ProbabilitySum[i + 1] = sum;
            }
        }
        #endregion

        #region Public methods.
        /// <summary>
        /// Draw a random integer from {0, .., n-1} according to previously
        /// supplied probability distribution. Time-complexity is O(n),
        /// where n is the length of the probabilities-array supplied to
        /// CreateIndexDistribution().
        /// Use this function if you make repeated draws from the same
        /// set and the set-size is small.
        /// </summary>
        /// <returns></returns>
        public int DrawLinearSearch()
        {
            double r = Rand.Uniform();

            int i;

            for (i = 0; ProbabilitySum[i] < r && i < ProbabilitySum.Length-1; i++)
            {
            }

            return i - 1;
        }

        /// <summary>
        /// Draw a random integer from {0, .., n-1} according to previously
        /// supplied probability distribution. Time-complexity is O(log(n)),
        /// where n is the length of the probabilities-array supplied to
        /// CreateIndexDistribution().
        /// Use this function if you make repeated draws from the same
        /// set and the set-size is large.
        /// </summary>
        public int DrawBinarySearch()
        {
            double r = Rand.Uniform();
            int minIdx=0;
            int maxIdx = ProbabilitySum.Length - 1;

            while (minIdx < maxIdx - 1)
            {
                int middle = (maxIdx - minIdx) / 2 + minIdx;

                if (r < ProbabilitySum[middle])
                {
                    maxIdx = middle;
                }
                else
                {
                    minIdx = middle;
                }
            }

            return minIdx;
        }
        #endregion

        #region Public properties.
        /// <summary>
        /// RNG object being used.
        /// </summary>
        public Random Rand
        {
            get;
            set;
        }
        #endregion

        #region Internal variables.
        /// <summary>
        /// Intermediate summation of the probability-distribution.
        /// </summary>
        private double[] ProbabilitySum;
        #endregion
    }
}