forked from tucano/UnityRandom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityRandom.cs
More file actions
265 lines (229 loc) · 7.51 KB
/
UnityRandom.cs
File metadata and controls
265 lines (229 loc) · 7.51 KB
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
256
257
258
259
260
261
262
263
264
265
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using NPack;
using URandom;
/////////////////////////////////////////////////////////////////////////////
// //
// Unity Random //
// //
// This code is free software under the Artistic license. //
// //
// distributions mainly from: http://www.nrbook.com/a/bookcpdf.php //
// //
// //
/////////////////////////////////////////////////////////////////////////////
public class UnityRandom
{
// STORE MAX SEED VALUE (no access to System)
public static int max_seed = Int32.MaxValue;
public enum Normalization
{
STDNORMAL = 0,
POWERLAW = 1
}
private MersenneTwister _rand;
// THE CONSTRUCTORS
public UnityRandom()
{
_rand = new MersenneTwister();
}
public UnityRandom(int seed)
{
_rand = new MersenneTwister(seed);
}
// VALUE Return a Float 0 - 1
public float Value()
{
return _rand.NextSingle(true);
}
// VALUE Return a Float 0 - 1
public float Value( Normalization n , float t)
{
if (n == Normalization.STDNORMAL) {
return (float) NormalDistribution.Normalize(_rand.NextSingle(true), t);
} else if (n == Normalization.POWERLAW) {
return (float) PowerLaw.Normalize(_rand.NextSingle(true), t, 0, 1);
} else {
return _rand.NextSingle(true);
}
}
// RANGE Return a Float min < x < max
public float Range(Int32 minValue, Int32 maxValue)
{
return _rand.Next(minValue, maxValue);
}
// RANGE Return a Float min < x < max
public float Range(Int32 minValue, Int32 maxValue, Normalization n, float t)
{
if (n == Normalization.STDNORMAL) {
return SpecialFunctions.ScaleFloatToRange( (float) NormalDistribution.Normalize(_rand.NextSingle(true), t), minValue, maxValue, 0, 1);
} else if (n == Normalization.POWERLAW) {
return (float) PowerLaw.Normalize(_rand.NextSingle(true), t, minValue, maxValue);
} else {
return _rand.Next(minValue, maxValue);
}
}
// POISSON Return a Float
public float Possion(float lambda)
{
return PoissonDistribution.Normalize( ref _rand, lambda);
}
// EXPONENTIAL Return a Float
public float Exponential(float lambda)
{
return ExponentialDistribution.Normalize( _rand.NextSingle( false ), lambda );
}
// GAMMA Return a Float
public float Gamma(float order)
{
return GammaDistribution.Normalize(ref _rand, (int) order);
}
// POINT IN A SQUARE Return a Vector2
public Vector2 PointInASquare()
{
return RandomSquare.Area(ref _rand);
}
// POINT IN A SQUARE Return a Vector2
public Vector2 PointInASquare(Normalization n , float t )
{
return RandomSquare.Area(ref _rand, n, t);
}
// RANDOM POINT IN A CIRCLE centered at 0
// FROM http://mathworld.wolfram.com/CirclePointPicking.html
// Take a number between 0 and 2PI and move to Cartesian Coordinates
public Vector2 PointInACircle()
{
return RandomDisk.Circle(ref _rand);
}
// RANDOM POINT IN A CIRCLE centered at 0
// FROM http://mathworld.wolfram.com/CirclePointPicking.html
// Take a number between 0 and 2PI and move to Cartesian Coordinates
public Vector2 PointInACircle(Normalization n, float t)
{
return RandomDisk.Circle(ref _rand, n, t);
}
// RANDOM POINT in a DISK
// FROM http://mathworld.wolfram.com/DiskPointPicking.html
public Vector2 PointInADisk()
{
return RandomDisk.Disk(ref _rand);
}
// RANDOM POINT in a DISK
// FROM http://mathworld.wolfram.com/DiskPointPicking.html
public Vector2 PointInADisk(Normalization n, float t)
{
return RandomDisk.Disk(ref _rand, n, t);
}
// RANDOM POINT IN A CUBE. Return a Vector3
public Vector3 PointInACube()
{
return RandomCube.Volume(ref _rand);
}
// RANDOM POINT IN A CUBE. Return a Vector3
public Vector3 PointInACube(Normalization n, float t)
{
return RandomCube.Volume(ref _rand, n, t);
}
// RANDOM POINT ON A CUBE. Return a Vector3
public Vector3 PointOnACube()
{
return RandomCube.Surface(ref _rand);
}
// RANDOM POINT ON A CUBE. Return a Vector3
public Vector3 PointOnACube(Normalization n, float t)
{
return RandomCube.Surface(ref _rand, n, t);
}
// RANDOM POINT ON A SPHERE. Return a Vector3
public Vector3 PointOnASphere()
{
return RandomSphere.Surface(ref _rand);
}
// RANDOM POINT ON A SPHERE. Return a Vector3
public Vector3 PointOnASphere(Normalization n, float t)
{
throw new ArgumentException("Normalizations for Sphere is not yet implemented");
}
// RANDOM POINT IN A SPHERE. Return a Vector3
public Vector3 PointInASphere()
{
return RandomSphere.Volume(ref _rand);
}
// RANDOM POINT IN A SPHERE. Return a Vector3
public Vector3 PointInASphere(Normalization n, float t)
{
throw new ArgumentException("Normalizations for Sphere is not yet implemented");
}
// RANDOM POINT IN A CAP. Return a Vector3
// TODO: see RandomSphere GetPointOnCap(float spotAngle, ref NPack.MersenneTwister _rand, Quaternion orientation)
public Vector3 PointOnCap(float spotAngle)
{
return RandomSphere.GetPointOnCap(spotAngle, ref _rand);
}
public Vector3 PointOnCap(float spotAngle, Normalization n, float t)
{
throw new ArgumentException("Normalizations for PointOnCap is not yet implemented");
}
// RANDOM POINT IN A RING on a SPHERE. Return a Vector3
// TODO: see RandomSphere public static Vector3 GetPointOnRing(float innerSpotAngle, float outerSpotAngle, ref NPack.MersenneTwister _rand, Quaternion orientation)
public Vector3 PointOnRing(float innerAngle, float outerAngle)
{
return RandomSphere.GetPointOnRing(innerAngle, outerAngle, ref _rand);
}
public Vector3 PointOnRing(float innerAngle, float outerAngle, Normalization n, float t)
{
throw new ArgumentException("Normalizations for PointOnRing is not yet implemented");
}
// RANDOM RAINBOW COLOR
public Color Rainbow()
{
return WaveToRgb.LinearToRgb(_rand.NextSingle(true));
}
// RANDOM RAINBOW COLOR
public Color Rainbow(Normalization n, float t)
{
if (n == Normalization.STDNORMAL) {
return WaveToRgb.LinearToRgb ( (float) NormalDistribution.Normalize(_rand.NextSingle(true), t));
} else if (n == Normalization.POWERLAW) {
return WaveToRgb.LinearToRgb ( (float) PowerLaw.Normalize(_rand.NextSingle(true), t, 0, 1));
} else {
return WaveToRgb.LinearToRgb(_rand.NextSingle(true));
}
}
// RANDOM DICES
public DiceRoll RollDice(int size, DiceRoll.DiceType type)
{
DiceRoll roll = new DiceRoll(size, type, ref _rand);
//Debug.Log(roll.TypeToString());
//Debug.Log(roll.RollToString());
//Debug.Log(roll.Sum());
return roll;
}
// START a FLOAT SHUFFLE BAG
// Note the a value can be shuffled with himself
public ShuffleBagCollection<float> ShuffleBag(float[] values)
{
ShuffleBagCollection<float> bag = new ShuffleBagCollection<float>();
foreach (float x in values)
{
bag.Add(x);
}
return bag;
}
// START a WIGHTED FLOAT SHUFFLE BAG, the trick is the it is added many times
// Note the a value can be shuffled with himself
public ShuffleBagCollection<float> ShuffleBag(Dictionary<float,int> dict)
{
ShuffleBagCollection<float> bag = new ShuffleBagCollection<float>();
foreach (KeyValuePair<float, int> x in dict)
{
//Debug.Log(x.Value);
int val = x.Value;
float key = x.Key;
bag.Add( key, val);
}
return bag;
}
}