forked from Unity-Technologies/Unity.Mathematics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.cs
More file actions
488 lines (427 loc) · 18.5 KB
/
random.cs
File metadata and controls
488 lines (427 loc) · 18.5 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
using System;
using Unity.Mathematics.Experimental;
using System.Runtime.CompilerServices;
using static Unity.Mathematics.math;
using System.Diagnostics;
// Random Number Generator based on xorshift.
// Designed for minimal state (32bits) to be easily embeddable into components.
// Core functionality is integer multiplication free to improve vectorization
// on less capable SIMD instruction sets.
namespace Unity.Mathematics
{
[Serializable]
public partial struct Random
{
public uint state;
// Seed must be non-zero
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Random(uint seed = 0x6E624EB7u)
{
CheckInitState();
state = seed;
NextState();
}
// Seed must be non-zero
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitState(uint seed = 0x6E624EB7u)
{
state = seed;
NextState();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool NextBool()
{
return (NextState() & 1) == 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool2 NextBool2()
{
uint v = NextState();
return (uint2(v) & uint2(1, 2)) == 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool3 NextBool3()
{
uint v = NextState();
return (uint3(v) & uint3(1, 2, 4)) == 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool4 NextBool4()
{
uint v = NextState();
return (uint4(v) & uint4(1, 2, 4, 8)) == 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int NextInt() // [-2147483647, 2147483647]
{
return (int)NextState() ^ -2147483648;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int2 NextInt2() // [-2147483647, 2147483647]
{
return int2((int)NextState(), (int)NextState()) ^ -2147483648;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int3 NextInt3() // [-2147483647, 2147483647]
{
return int3((int)NextState(), (int)NextState(), (int)NextState()) ^ -2147483648;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int4 NextInt4() // [-2147483647, 2147483647]
{
return int4((int)NextState(), (int)NextState(), (int)NextState(), (int)NextState()) ^ -2147483648;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int NextInt(int max) // [0, max)
{
CheckNextIntMax(max);
return (int)((NextState() * (ulong)max) >> 32);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int2 NextInt2(int2 max) // [0, max)
{
CheckNextIntMax(max.x);
CheckNextIntMax(max.y);
return int2((int)(NextState() * (ulong)max.x >> 32),
(int)(NextState() * (ulong)max.y >> 32));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int3 NextInt3(int3 max) // [0, max)
{
CheckNextIntMax(max.x);
CheckNextIntMax(max.y);
CheckNextIntMax(max.z);
return int3((int)(NextState() * (ulong)max.x >> 32),
(int)(NextState() * (ulong)max.y >> 32),
(int)(NextState() * (ulong)max.z >> 32));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int4 NextInt4(int4 max) // [0, max)
{
CheckNextIntMax(max.x);
CheckNextIntMax(max.y);
CheckNextIntMax(max.z);
CheckNextIntMax(max.w);
return int4((int)(NextState() * (ulong)max.x >> 32),
(int)(NextState() * (ulong)max.y >> 32),
(int)(NextState() * (ulong)max.z >> 32),
(int)(NextState() * (ulong)max.w >> 32));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int NextInt(int min, int max) //[min, max)
{
CheckNextIntMinMax(min, max);
uint range = (uint)(max - min);
return (int)(NextState() * (ulong)range >> 32) + min;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int2 NextInt2(int2 min, int2 max) //[min, max)
{
CheckNextIntMinMax(min.x, max.x);
CheckNextIntMinMax(min.y, max.y);
uint2 range = (uint2)(max - min);
return int2((int)(NextState() * (ulong)range.x >> 32),
(int)(NextState() * (ulong)range.y >> 32)) + min;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int3 NextInt3(int3 min, int3 max) //[min, max)
{
CheckNextIntMinMax(min.x, max.x);
CheckNextIntMinMax(min.y, max.y);
CheckNextIntMinMax(min.z, max.z);
uint3 range = (uint3)(max - min);
return int3((int)(NextState() * (ulong)range.x >> 32),
(int)(NextState() * (ulong)range.y >> 32),
(int)(NextState() * (ulong)range.z >> 32)) + min;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int4 NextInt4(int4 min, int4 max) //[min, max)
{
CheckNextIntMinMax(min.x, max.x);
CheckNextIntMinMax(min.y, max.y);
CheckNextIntMinMax(min.z, max.z);
CheckNextIntMinMax(min.w, max.w);
uint4 range = (uint4)(max - min);
return int4((int)(NextState() * (ulong)range.x >> 32),
(int)(NextState() * (ulong)range.y >> 32),
(int)(NextState() * (ulong)range.z >> 32),
(int)(NextState() * (ulong)range.w >> 32)) + min;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint NextUInt() // [0, 4294967294]
{
return NextState() - 1u;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint2 NextUInt2() // [0, 4294967294]
{
return uint2(NextState(), NextState()) - 1u;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint3 NextUInt3() // [0, 4294967294]
{
return uint3(NextState(), NextState(), NextState()) - 1u;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint4 NextUInt4() // [0, 4294967294]
{
return uint4(NextState(), NextState(), NextState(), NextState()) - 1u;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint NextUInt(uint max) // [0, max)
{
return (uint)((NextState() * (ulong)max) >> 32);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint2 NextUInt2(uint2 max) // [0, max)
{
return uint2( (uint)(NextState() * (ulong)max.x >> 32),
(uint)(NextState() * (ulong)max.y >> 32));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint3 NextUInt3(uint3 max) // [0, max)
{
return uint3( (uint)(NextState() * (ulong)max.x >> 32),
(uint)(NextState() * (ulong)max.y >> 32),
(uint)(NextState() * (ulong)max.z >> 32));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint4 NextUInt4(uint4 max) // [0, max)
{
return uint4( (uint)(NextState() * (ulong)max.x >> 32),
(uint)(NextState() * (ulong)max.y >> 32),
(uint)(NextState() * (ulong)max.z >> 32),
(uint)(NextState() * (ulong)max.w >> 32));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint NextUInt(uint min, uint max) //[min, max)
{
CheckNextUIntMinMax(min, max);
uint range = max - min;
return (uint)(NextState() * (ulong)range >> 32) + min;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint2 NextUInt2(uint2 min, uint2 max) //[min, max)
{
CheckNextUIntMinMax(min.x, max.x);
CheckNextUIntMinMax(min.y, max.y);
uint2 range = max - min;
return uint2((uint)(NextState() * (ulong)range.x >> 32),
(uint)(NextState() * (ulong)range.y >> 32)) + min;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint3 NextUInt3(uint3 min, uint3 max) //[min, max)
{
CheckNextUIntMinMax(min.x, max.x);
CheckNextUIntMinMax(min.y, max.y);
CheckNextUIntMinMax(min.z, max.z);
uint3 range = max - min;
return uint3((uint)(NextState() * (ulong)range.x >> 32),
(uint)(NextState() * (ulong)range.y >> 32),
(uint)(NextState() * (ulong)range.z >> 32)) + min;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint4 NextUInt4(uint4 min, uint4 max) //[min, max)
{
CheckNextUIntMinMax(min.x, max.x);
CheckNextUIntMinMax(min.y, max.y);
CheckNextUIntMinMax(min.z, max.z);
CheckNextUIntMinMax(min.w, max.w);
uint4 range = (uint4)(max - min);
return uint4((uint)(NextState() * (ulong)range.x >> 32),
(uint)(NextState() * (ulong)range.y >> 32),
(uint)(NextState() * (ulong)range.z >> 32),
(uint)(NextState() * (ulong)range.w >> 32)) + min;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float NextFloat() // [0, 1)
{
return asfloat(0x3f80_0000 | (NextState() >> 9)) - 1.0f;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float2 NextFloat2() // [0, 1)
{
return asfloat(0x3f80_0000 | (uint2(NextState(), NextState()) >> 9)) - 1.0f;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float3 NextFloat3() // [0, 1)
{
return asfloat(0x3f80_0000 | (uint3(NextState(), NextState(), NextState()) >> 9)) - 1.0f;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float4 NextFloat4() // [0, 1)
{
return asfloat(0x3f80_0000 | (uint4(NextState(), NextState(), NextState(), NextState()) >> 9)) - 1.0f;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float NextFloat(float max) { return NextFloat() * max; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float2 NextFloat2(float2 max) { return NextFloat2() * max; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float3 NextFloat3(float3 max) { return NextFloat3() * max; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float4 NextFloat4(float4 max) { return NextFloat4() * max; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float NextFloat(float min, float max) { return NextFloat() * (max - min) + min; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float2 NextFloat2(float2 min, float2 max) { return NextFloat2() * (max - min) + min; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float3 NextFloat3(float3 min, float3 max) { return NextFloat3() * (max - min) + min; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float4 NextFloat4(float4 min, float4 max) { return NextFloat4() * (max - min) + min; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double NextDouble() // [0, 1)
{
ulong sx = ((ulong)NextState() << 20) ^ NextState();
return asdouble(0x3ff0_0000_0000_0000 | sx) - 1.0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double2 NextDouble2() // [0, 1)
{
ulong sx = ((ulong)NextState() << 20) ^ NextState();
ulong sy = ((ulong)NextState() << 20) ^ NextState();
return double2(asdouble(0x3ff0_0000_0000_0000 | sx),
asdouble(0x3ff0_0000_0000_0000 | sy)) - 1.0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double3 NextDouble3() // [0, 1)
{
ulong sx = ((ulong)NextState() << 20) ^ NextState();
ulong sy = ((ulong)NextState() << 20) ^ NextState();
ulong sz = ((ulong)NextState() << 20) ^ NextState();
return double3(asdouble(0x3ff0_0000_0000_0000 | sx),
asdouble(0x3ff0_0000_0000_0000 | sy),
asdouble(0x3ff0_0000_0000_0000 | sz)) - 1.0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double4 NextDouble4() // [0, 1)
{
ulong sx = ((ulong)NextState() << 20) ^ NextState();
ulong sy = ((ulong)NextState() << 20) ^ NextState();
ulong sz = ((ulong)NextState() << 20) ^ NextState();
ulong sw = ((ulong)NextState() << 20) ^ NextState();
return double4(asdouble(0x3ff0_0000_0000_0000 | sx),
asdouble(0x3ff0_0000_0000_0000 | sy),
asdouble(0x3ff0_0000_0000_0000 | sz),
asdouble(0x3ff0_0000_0000_0000 | sw)) - 1.0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double NextDouble(double max) { return NextDouble() * max; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double2 NextDouble2(double2 max) { return NextDouble2() * max; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double3 NextDouble3(double3 max) { return NextDouble3() * max; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double4 NextDouble4(double4 max) { return NextDouble4() * max; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double NextDouble(double min, double max) { return NextDouble() * (max - min) + min; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double2 NextDouble2(double2 min, double2 max) { return NextDouble2() * (max - min) + min; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double3 NextDouble3(double3 min, double3 max) { return NextDouble3() * (max - min) + min; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double4 NextDouble4(double4 min, double4 max) { return NextDouble4() * (max - min) + min; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float2 NextFloat2Direction()
{
float angle = NextFloat() * (float)PI * 2.0f;
float s, c;
sincos(angle, out s, out c);
return float2(c, s);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double2 NextDouble2Direction()
{
double angle = NextDouble() * PI * 2.0;
double s, c;
sincos(angle, out s, out c);
return double2(c, s);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float3 NextFloat3Direction()
{
float2 rnd = NextFloat2();
float z = rnd.x * 2.0f - 1.0f;
float r = sqrt(max(1.0f - z * z, 0.0f));
float angle = rnd.y * (float)PI * 2.0f;
float s, c;
sincos(angle, out s, out c);
return float3(c*r, s*r, z);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double3 NextDouble3Direction()
{
double2 rnd = NextDouble2();
double z = rnd.x * 2.0 - 1.0;
double r = sqrt(max(1.0 - z * z, 0.0));
double angle = rnd.y * PI * 2.0;
double s, c;
sincos(angle, out s, out c);
return double3(c * r, s * r, z);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public quaternion NextQuaternionRotation() // Uniformly random rotation
{
float3 rnd = NextFloat3(float3(2.0f * (float)PI, 2.0f * (float)PI, 1.0f));
float u1 = rnd.z;
float2 theta_rho = rnd.xy;
float i = sqrt(1.0f - u1);
float j = sqrt(u1);
float2 sin_theta_rho;
float2 cos_theta_rho;
sincos(theta_rho, out sin_theta_rho, out cos_theta_rho);
quaternion q = quaternion(i * sin_theta_rho.x, i * cos_theta_rho.x, j * sin_theta_rho.y, j * cos_theta_rho.y);
return quaternion(select(q.value, -q.value, q.value.w < 0.0f));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private uint NextState()
{
CheckState();
uint t = state;
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
return t;
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
private void CheckInitState()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (state == 0)
throw new System.ArgumentException("Seed must be non-zero");
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
private void CheckState()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if(state == 0)
throw new System.ArgumentException("Invalid state 0. Random object has not been properly initialized");
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
private void CheckNextIntMax(int max)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (max < 0)
throw new System.ArgumentException("max must be positive");
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
private void CheckNextIntMinMax(int min, int max)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (min > max)
throw new System.ArgumentException("min must be less than or equal to max");
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
private void CheckNextUIntMinMax(uint min, uint max)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (min > max)
throw new System.ArgumentException("min must be less than or equal to max");
#endif
}
}
}