forked from ZorPastaman/Random-Generators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightedDistribution.cs
195 lines (177 loc) · 6.79 KB
/
WeightedDistribution.cs
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
// Copyright (c) 2020-2021 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Random-Generators
using System;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using Zor.RandomGenerators.ContinuousDistributions;
using Random = UnityEngine.Random;
namespace Zor.RandomGenerators.DiscreteDistributions
{
/// <summary>
/// Collection of methods that generate a random value using Weighted distribution algorithms.
/// </summary>
public static class WeightedDistribution
{
/// <summary>
/// Generates a random value using <see cref="Random.value"/> as an iid source.
/// </summary>
/// <param name="weights">Weights array.</param>
/// <returns>Index of a gotten value from <paramref name="weights"/>.</returns>
/// <remarks>
/// Count of <paramref name="weights"/> must be greater than 0.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public static int Generate([NotNull] uint[] weights)
{
int count = weights.Length;
uint sum = ComputeSum(weights, count);
return Pop(weights, sum, count, Random.value);
}
/// <summary>
/// Generates a random value using <see cref="Random.value"/> as an iid source.
/// </summary>
/// <param name="weights">Weights array.</param>
/// <param name="setup">Precomputed data.</param>
/// <returns>Index of a gotten value from <paramref name="weights"/>.</returns>
/// <remarks>
/// It's a faster variant using a precomputed <paramref name="setup"/>.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public static int Generate([NotNull] uint[] weights, Setup setup)
{
return Pop(weights, setup.sum, setup.count, Random.value);
}
/// <summary>
/// Generates a random value using <paramref name="iidFunc"/> as an iid source.
/// </summary>
/// <param name="iidFunc">
/// Function that returns an independent and identically distributed random value in range [0, 1].
/// </param>
/// <param name="weights">Weights array.</param>
/// <returns>Index of a gotten value from <paramref name="weights"/>.</returns>
/// <remarks>
/// Count of <paramref name="weights"/> must be greater than 0.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public static int Generate([NotNull] Func<float> iidFunc, [NotNull] uint[] weights)
{
int count = weights.Length;
uint sum = ComputeSum(weights, count);
return Pop(weights, sum, count, iidFunc());
}
/// <summary>
/// Generates a random value using <paramref name="iidFunc"/> as an iid source.
/// </summary>
/// <param name="iidFunc">
/// Function that returns an independent and identically distributed random value in range [0, 1].
/// </param>
/// <param name="weights">Weights array.</param>
/// <param name="setup">Precomputed data.</param>
/// <returns>Index of a gotten value from <paramref name="weights"/>.</returns>
/// <remarks>
/// It's a faster variant using a precomputed <paramref name="setup"/>.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public static int Generate([NotNull] Func<float> iidFunc, [NotNull] uint[] weights, Setup setup)
{
return Pop(weights, setup.sum, setup.count, iidFunc());
}
/// <summary>
/// Generates a random value using <paramref name="iidGenerator"/> as an iid source.
/// </summary>
/// <param name="iidGenerator">
/// Random generator that returns an independent and identically distributed random value in range [0, 1].
/// </param>
/// <param name="weights">Weights array.</param>
/// <returns>Index of a gotten value from <paramref name="weights"/>.</returns>
/// <remarks>
/// Count of <paramref name="weights"/> must be greater than 0.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public static int Generate<T>([NotNull] T iidGenerator, [NotNull] uint[] weights)
where T : IContinuousGenerator
{
int count = weights.Length;
uint sum = ComputeSum(weights, count);
return Pop(weights, sum, count, iidGenerator.Generate());
}
/// <summary>
/// Generates a random value using <paramref name="iidGenerator"/> as an iid source.
/// </summary>
/// <param name="iidGenerator">
/// Random generator that returns an independent and identically distributed random value in range [0, 1].
/// </param>
/// <param name="weights">Weights array.</param>
/// <param name="setup">Precomputed data.</param>
/// <returns>Index of a gotten value from <paramref name="weights"/>.</returns>
/// <remarks>
/// It's a faster variant using a precomputed <paramref name="setup"/>.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public static int Generate<T>([NotNull] T iidGenerator, [NotNull] uint[] weights, Setup setup)
where T : IContinuousGenerator
{
return Pop(weights, setup.sum, setup.count, iidGenerator.Generate());
}
/// <summary>
/// Computes sum of first <paramref name="count"/> elements of <paramref name="weights"/>.
/// </summary>
/// <param name="weights">Weights array.</param>
/// <param name="count">How many first elements are summed.</param>
/// <returns>Computed sum.</returns>
[Pure]
private static uint ComputeSum([NotNull] uint[] weights, int count)
{
uint sum = 0u;
for (int i = 0; i < count; ++i)
{
sum += weights[i];
}
return sum;
}
/// <summary>
/// Pops index of <paramref name="weights"/> that corresponds to <paramref name="iid"/>.
/// </summary>
/// <param name="weights">Weights array.</param>
/// <param name="sum">Sum of weights in <paramref name="weights"/>.</param>
/// <param name="count">Count of <paramref name="weights"/>.</param>
/// <param name="iid">Iid in range [0, 1].</param>
/// <returns>Index of popped value.</returns>
/// <remarks>
/// <paramref name="count"/> must be greater than 0.
/// </remarks>
[Pure]
private static int Pop([NotNull] uint[] weights, uint sum, int count, float iid)
{
uint random = (uint)(iid * sum);
sum = 0u;
int i = 0;
for (; sum <= random & i < count; ++i)
{
sum += weights[i];
}
return i - 1;
}
/// <summary>
/// Precomputed setup data. It's used in optimized methods in <see cref="WeightedDistribution"/>.
/// </summary>
/// <remarks>
/// <para>Never use the default constructor.</para>
/// <para>If you change weights, recreate <see cref="Setup"/>.</para>
/// </remarks>
public readonly struct Setup
{
public readonly uint sum;
public readonly int count;
/// <summary>
/// Creates <see cref="Setup"/> for optimized methods in <see cref="WeightedDistribution"/>.
/// </summary>
/// <param name="weights">Weights array. Its count must be greater than 0.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Setup([NotNull] uint[] weights)
{
count = weights.Length;
sum = ComputeSum(weights, count);
}
}
}
}