com.wallstop-studios.unity-helpers 2.0.0-rc06 → 2.0.0-rc08
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Runtime/Core/Extension/DirectionExtensions.cs +5 -2
- package/Runtime/Core/Extension/IEnumerableExtensions.cs +19 -6
- package/Runtime/Core/Extension/RandomExtensions.cs +9 -87
- package/Runtime/Core/Extension/UnityExtensions.cs +2 -2
- package/Runtime/Core/Helper/Helpers.cs +1 -556
- package/Runtime/Core/Helper/Partials/LogHelpers.cs +13 -0
- package/Runtime/Core/Helper/Partials/LogHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials/MathHelpers.cs +30 -0
- package/Runtime/Core/Helper/Partials/MathHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials/ObjectHelpers.cs +388 -0
- package/Runtime/Core/Helper/Partials/ObjectHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials/TransformHelpers.cs +167 -0
- package/Runtime/Core/Helper/Partials/TransformHelpers.cs.meta +3 -0
- package/Runtime/Core/Helper/Partials.meta +3 -0
- package/Runtime/Core/Random/AbstractRandom.cs +140 -154
- package/Runtime/Core/Random/IRandom.cs +26 -7
- package/Runtime/Core/Random/PerlinNoise.cs +369 -0
- package/Runtime/Core/Random/PerlinNoise.cs.meta +3 -0
- package/Runtime/Core/Random/SquirrelRandom.cs +9 -10
- package/Runtime/Core/Random/SystemRandom.cs +78 -41
- package/Tests/Runtime/Extensions/RandomExtensionTests.cs +27 -0
- package/Tests/Runtime/Extensions/RandomExtensionTests.cs.meta +3 -0
- package/Tests/Runtime/Helper/ObjectHelperTests.cs +402 -0
- package/Tests/Runtime/Helper/ObjectHelperTests.cs.meta +3 -0
- package/Tests/Runtime/Performance/RandomPerformanceTests.cs +58 -3
- package/Tests/Runtime/Random/RandomTestBase.cs +557 -6
- package/Tests/Runtime/Random/SquirrelRandomTests.cs +5 -0
- package/package.json +1 -1
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
namespace UnityHelpers.Core.Random
|
|
2
|
+
{
|
|
3
|
+
using System;
|
|
4
|
+
using Extension;
|
|
5
|
+
using UnityEngine;
|
|
6
|
+
|
|
7
|
+
public sealed class PerlinNoise
|
|
8
|
+
{
|
|
9
|
+
// Permutation array. This is a standard permutation of numbers from 0 to 255.
|
|
10
|
+
private static readonly int[] DefaultPermutations =
|
|
11
|
+
{
|
|
12
|
+
151,
|
|
13
|
+
160,
|
|
14
|
+
137,
|
|
15
|
+
91,
|
|
16
|
+
90,
|
|
17
|
+
15,
|
|
18
|
+
131,
|
|
19
|
+
13,
|
|
20
|
+
201,
|
|
21
|
+
95,
|
|
22
|
+
96,
|
|
23
|
+
53,
|
|
24
|
+
194,
|
|
25
|
+
233,
|
|
26
|
+
7,
|
|
27
|
+
225,
|
|
28
|
+
140,
|
|
29
|
+
36,
|
|
30
|
+
103,
|
|
31
|
+
30,
|
|
32
|
+
69,
|
|
33
|
+
142,
|
|
34
|
+
8,
|
|
35
|
+
99,
|
|
36
|
+
37,
|
|
37
|
+
240,
|
|
38
|
+
21,
|
|
39
|
+
10,
|
|
40
|
+
23,
|
|
41
|
+
190,
|
|
42
|
+
6,
|
|
43
|
+
148,
|
|
44
|
+
247,
|
|
45
|
+
120,
|
|
46
|
+
234,
|
|
47
|
+
75,
|
|
48
|
+
0,
|
|
49
|
+
26,
|
|
50
|
+
197,
|
|
51
|
+
62,
|
|
52
|
+
94,
|
|
53
|
+
252,
|
|
54
|
+
219,
|
|
55
|
+
203,
|
|
56
|
+
117,
|
|
57
|
+
35,
|
|
58
|
+
11,
|
|
59
|
+
32,
|
|
60
|
+
57,
|
|
61
|
+
177,
|
|
62
|
+
33,
|
|
63
|
+
88,
|
|
64
|
+
237,
|
|
65
|
+
149,
|
|
66
|
+
56,
|
|
67
|
+
87,
|
|
68
|
+
174,
|
|
69
|
+
20,
|
|
70
|
+
125,
|
|
71
|
+
136,
|
|
72
|
+
171,
|
|
73
|
+
168,
|
|
74
|
+
68,
|
|
75
|
+
175,
|
|
76
|
+
74,
|
|
77
|
+
165,
|
|
78
|
+
71,
|
|
79
|
+
134,
|
|
80
|
+
139,
|
|
81
|
+
48,
|
|
82
|
+
27,
|
|
83
|
+
166,
|
|
84
|
+
77,
|
|
85
|
+
146,
|
|
86
|
+
158,
|
|
87
|
+
231,
|
|
88
|
+
83,
|
|
89
|
+
111,
|
|
90
|
+
229,
|
|
91
|
+
122,
|
|
92
|
+
60,
|
|
93
|
+
211,
|
|
94
|
+
133,
|
|
95
|
+
230,
|
|
96
|
+
220,
|
|
97
|
+
105,
|
|
98
|
+
92,
|
|
99
|
+
41,
|
|
100
|
+
55,
|
|
101
|
+
46,
|
|
102
|
+
245,
|
|
103
|
+
40,
|
|
104
|
+
244,
|
|
105
|
+
102,
|
|
106
|
+
143,
|
|
107
|
+
54,
|
|
108
|
+
65,
|
|
109
|
+
25,
|
|
110
|
+
63,
|
|
111
|
+
161,
|
|
112
|
+
1,
|
|
113
|
+
216,
|
|
114
|
+
80,
|
|
115
|
+
73,
|
|
116
|
+
209,
|
|
117
|
+
76,
|
|
118
|
+
132,
|
|
119
|
+
187,
|
|
120
|
+
208,
|
|
121
|
+
89,
|
|
122
|
+
18,
|
|
123
|
+
169,
|
|
124
|
+
200,
|
|
125
|
+
196,
|
|
126
|
+
135,
|
|
127
|
+
130,
|
|
128
|
+
116,
|
|
129
|
+
188,
|
|
130
|
+
159,
|
|
131
|
+
86,
|
|
132
|
+
164,
|
|
133
|
+
100,
|
|
134
|
+
109,
|
|
135
|
+
198,
|
|
136
|
+
173,
|
|
137
|
+
186,
|
|
138
|
+
3,
|
|
139
|
+
64,
|
|
140
|
+
52,
|
|
141
|
+
217,
|
|
142
|
+
226,
|
|
143
|
+
250,
|
|
144
|
+
124,
|
|
145
|
+
123,
|
|
146
|
+
5,
|
|
147
|
+
202,
|
|
148
|
+
38,
|
|
149
|
+
147,
|
|
150
|
+
118,
|
|
151
|
+
126,
|
|
152
|
+
255,
|
|
153
|
+
82,
|
|
154
|
+
85,
|
|
155
|
+
212,
|
|
156
|
+
207,
|
|
157
|
+
206,
|
|
158
|
+
59,
|
|
159
|
+
227,
|
|
160
|
+
47,
|
|
161
|
+
16,
|
|
162
|
+
58,
|
|
163
|
+
17,
|
|
164
|
+
182,
|
|
165
|
+
189,
|
|
166
|
+
28,
|
|
167
|
+
42,
|
|
168
|
+
223,
|
|
169
|
+
183,
|
|
170
|
+
170,
|
|
171
|
+
213,
|
|
172
|
+
119,
|
|
173
|
+
248,
|
|
174
|
+
152,
|
|
175
|
+
2,
|
|
176
|
+
44,
|
|
177
|
+
154,
|
|
178
|
+
163,
|
|
179
|
+
70,
|
|
180
|
+
221,
|
|
181
|
+
153,
|
|
182
|
+
101,
|
|
183
|
+
155,
|
|
184
|
+
167,
|
|
185
|
+
43,
|
|
186
|
+
172,
|
|
187
|
+
9,
|
|
188
|
+
129,
|
|
189
|
+
22,
|
|
190
|
+
39,
|
|
191
|
+
253,
|
|
192
|
+
19,
|
|
193
|
+
98,
|
|
194
|
+
108,
|
|
195
|
+
110,
|
|
196
|
+
79,
|
|
197
|
+
113,
|
|
198
|
+
224,
|
|
199
|
+
232,
|
|
200
|
+
178,
|
|
201
|
+
185,
|
|
202
|
+
112,
|
|
203
|
+
104,
|
|
204
|
+
218,
|
|
205
|
+
246,
|
|
206
|
+
97,
|
|
207
|
+
228,
|
|
208
|
+
251,
|
|
209
|
+
34,
|
|
210
|
+
242,
|
|
211
|
+
193,
|
|
212
|
+
238,
|
|
213
|
+
210,
|
|
214
|
+
144,
|
|
215
|
+
12,
|
|
216
|
+
191,
|
|
217
|
+
179,
|
|
218
|
+
162,
|
|
219
|
+
241,
|
|
220
|
+
81,
|
|
221
|
+
51,
|
|
222
|
+
145,
|
|
223
|
+
235,
|
|
224
|
+
249,
|
|
225
|
+
14,
|
|
226
|
+
239,
|
|
227
|
+
107,
|
|
228
|
+
49,
|
|
229
|
+
192,
|
|
230
|
+
214,
|
|
231
|
+
31,
|
|
232
|
+
181,
|
|
233
|
+
199,
|
|
234
|
+
106,
|
|
235
|
+
157,
|
|
236
|
+
184,
|
|
237
|
+
84,
|
|
238
|
+
204,
|
|
239
|
+
176,
|
|
240
|
+
115,
|
|
241
|
+
121,
|
|
242
|
+
50,
|
|
243
|
+
45,
|
|
244
|
+
127,
|
|
245
|
+
4,
|
|
246
|
+
150,
|
|
247
|
+
254,
|
|
248
|
+
138,
|
|
249
|
+
236,
|
|
250
|
+
205,
|
|
251
|
+
93,
|
|
252
|
+
222,
|
|
253
|
+
114,
|
|
254
|
+
67,
|
|
255
|
+
29,
|
|
256
|
+
24,
|
|
257
|
+
72,
|
|
258
|
+
243,
|
|
259
|
+
141,
|
|
260
|
+
128,
|
|
261
|
+
195,
|
|
262
|
+
78,
|
|
263
|
+
66,
|
|
264
|
+
215,
|
|
265
|
+
61,
|
|
266
|
+
156,
|
|
267
|
+
180,
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
public static readonly PerlinNoise Instance = new();
|
|
271
|
+
|
|
272
|
+
private readonly int[] _permutations = new int[DefaultPermutations.Length];
|
|
273
|
+
|
|
274
|
+
// Doubled permutation to avoid overflow
|
|
275
|
+
private readonly int[] _doubledPermutations = new int[DefaultPermutations.Length * 2];
|
|
276
|
+
|
|
277
|
+
public PerlinNoise()
|
|
278
|
+
: this(null) { }
|
|
279
|
+
|
|
280
|
+
// Static constructor to initialize the doubled permutation array
|
|
281
|
+
public PerlinNoise(IRandom random)
|
|
282
|
+
{
|
|
283
|
+
Array.Copy(DefaultPermutations, 0, _permutations, 0, DefaultPermutations.Length);
|
|
284
|
+
if (random != null)
|
|
285
|
+
{
|
|
286
|
+
_permutations.Shuffle(random);
|
|
287
|
+
}
|
|
288
|
+
for (int i = 0; i < _doubledPermutations.Length; ++i)
|
|
289
|
+
{
|
|
290
|
+
_doubledPermutations[i] = _permutations[i % _permutations.Length];
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Fade function as defined by Ken Perlin. This eases coordinate values
|
|
295
|
+
// so that they will "ease" towards integral values. This ends up smoothing the final output.
|
|
296
|
+
public static float Fade(float t)
|
|
297
|
+
{
|
|
298
|
+
return t * t * t * (t * (t * 6 - 15) + 10);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Linear interpolation function
|
|
302
|
+
public static float Lerp(float t, float a, float b)
|
|
303
|
+
{
|
|
304
|
+
return a + t * (b - a);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Gradient function calculates the dot product between a pseudorandom gradient vector and the vector from the input coordinate to the grid coordinate
|
|
308
|
+
public static float Grad(int hash, float x, float y)
|
|
309
|
+
{
|
|
310
|
+
int h = hash & 7; // Convert low 3 bits of hash code
|
|
311
|
+
float u = h < 4 ? x : y; // If h < 4, use x, else use y
|
|
312
|
+
float v = h < 4 ? y : x; // If h < 4, use y, else use x
|
|
313
|
+
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// The Perlin noise function
|
|
317
|
+
public float Noise(float x, float y)
|
|
318
|
+
{
|
|
319
|
+
// Find unit grid cell containing point
|
|
320
|
+
int clampedX = (int)Mathf.Floor(x) & 255;
|
|
321
|
+
int clampedY = (int)Mathf.Floor(y) & 255;
|
|
322
|
+
|
|
323
|
+
// Get relative xy coordinates inside the cell
|
|
324
|
+
x -= Mathf.Floor(x);
|
|
325
|
+
y -= Mathf.Floor(y);
|
|
326
|
+
|
|
327
|
+
// Compute fade curves for x and y
|
|
328
|
+
float u = Fade(x);
|
|
329
|
+
float v = Fade(y);
|
|
330
|
+
|
|
331
|
+
// Hash coordinates of the square's corners
|
|
332
|
+
int aa = _doubledPermutations[_doubledPermutations[clampedX] + clampedY];
|
|
333
|
+
int ab = _doubledPermutations[_doubledPermutations[clampedX] + clampedY + 1];
|
|
334
|
+
int ba = _doubledPermutations[_doubledPermutations[clampedX + 1] + clampedY];
|
|
335
|
+
int bb = _doubledPermutations[_doubledPermutations[clampedX + 1] + clampedY + 1];
|
|
336
|
+
|
|
337
|
+
// Add blended results from the corners
|
|
338
|
+
float res = Lerp(
|
|
339
|
+
v,
|
|
340
|
+
Lerp(u, Grad(aa, x, y), Grad(ba, x - 1, y)),
|
|
341
|
+
Lerp(u, Grad(ab, x, y - 1), Grad(bb, x - 1, y - 1))
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
// Optional: Scale result to [0,1]
|
|
345
|
+
return (res + 1.0f) / 2.0f;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Optional: Generate noise with multiple octaves for more complexity
|
|
349
|
+
public float OctaveNoise(float x, float y, int octaves, float persistence)
|
|
350
|
+
{
|
|
351
|
+
float total = 0;
|
|
352
|
+
float frequency = 1;
|
|
353
|
+
float amplitude = 1;
|
|
354
|
+
float maxValue = 0; // Used for normalizing result to [0,1]
|
|
355
|
+
|
|
356
|
+
for (int i = 0; i < octaves; ++i)
|
|
357
|
+
{
|
|
358
|
+
total += Noise(x * frequency, y * frequency) * amplitude;
|
|
359
|
+
|
|
360
|
+
maxValue += amplitude;
|
|
361
|
+
|
|
362
|
+
amplitude *= persistence;
|
|
363
|
+
frequency *= 2;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return total / maxValue;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
|
|
38
38
|
public override uint NextUint()
|
|
39
39
|
{
|
|
40
|
-
return
|
|
40
|
+
return NextUintInternal(ref _position);
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
// Does not advance the RNG
|
|
@@ -51,16 +51,15 @@
|
|
|
51
51
|
return new SquirrelRandom(InternalState);
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
private static uint NextUintInternal(uint seed)
|
|
54
|
+
private static uint NextUintInternal(ref uint seed)
|
|
55
55
|
{
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return result;
|
|
56
|
+
seed *= BitNoise1;
|
|
57
|
+
seed ^= (seed >> 8);
|
|
58
|
+
seed += BitNoise2;
|
|
59
|
+
seed ^= (seed << 8);
|
|
60
|
+
seed *= BitNoise3;
|
|
61
|
+
seed ^= (seed >> 8);
|
|
62
|
+
return seed;
|
|
64
63
|
}
|
|
65
64
|
|
|
66
65
|
// https://youtu.be/LWFzPP8ZbdU?t=2906
|
|
@@ -12,14 +12,18 @@
|
|
|
12
12
|
[DataContract]
|
|
13
13
|
public sealed class SystemRandom : AbstractRandom
|
|
14
14
|
{
|
|
15
|
+
private const int HalfwayInt = int.MaxValue / 2;
|
|
16
|
+
private const int SeedArraySize = 56;
|
|
17
|
+
private const int LastSeedIndex = SeedArraySize - 1;
|
|
18
|
+
|
|
15
19
|
public static IRandom Instance => ThreadLocalRandom<SystemRandom>.Instance;
|
|
16
20
|
|
|
17
21
|
public override RandomState InternalState =>
|
|
18
22
|
new(
|
|
19
|
-
unchecked((ulong)
|
|
20
|
-
unchecked((ulong)
|
|
23
|
+
unchecked((ulong)_inext),
|
|
24
|
+
unchecked((ulong)_inextp),
|
|
21
25
|
_cachedGaussian,
|
|
22
|
-
ArrayConverter.IntArrayToByteArrayBlockCopy(
|
|
26
|
+
ArrayConverter.IntArrayToByteArrayBlockCopy(_seedArray)
|
|
23
27
|
);
|
|
24
28
|
|
|
25
29
|
/*
|
|
@@ -27,9 +31,9 @@
|
|
|
27
31
|
same across platforms, a fact which defeats the purpose of these serializable
|
|
28
32
|
randoms.
|
|
29
33
|
*/
|
|
30
|
-
private int
|
|
31
|
-
private int
|
|
32
|
-
private readonly int[]
|
|
34
|
+
private int _inext;
|
|
35
|
+
private int _inextp;
|
|
36
|
+
private readonly int[] _seedArray = new int[SeedArraySize];
|
|
33
37
|
|
|
34
38
|
public SystemRandom()
|
|
35
39
|
: this(Guid.NewGuid().GetHashCode()) { }
|
|
@@ -37,28 +41,34 @@
|
|
|
37
41
|
public SystemRandom(int seed)
|
|
38
42
|
{
|
|
39
43
|
int num1 = 161803398 - (seed == int.MinValue ? int.MaxValue : Math.Abs(seed));
|
|
40
|
-
|
|
44
|
+
_seedArray[LastSeedIndex] = num1;
|
|
41
45
|
int num2 = 1;
|
|
42
|
-
for (int index1 = 1; index1 <
|
|
46
|
+
for (int index1 = 1; index1 < LastSeedIndex; ++index1)
|
|
43
47
|
{
|
|
44
|
-
int index2 = 21 * index1 %
|
|
45
|
-
|
|
48
|
+
int index2 = 21 * index1 % LastSeedIndex;
|
|
49
|
+
_seedArray[index2] = num2;
|
|
46
50
|
num2 = num1 - num2;
|
|
47
51
|
if (num2 < 0)
|
|
52
|
+
{
|
|
48
53
|
num2 += int.MaxValue;
|
|
49
|
-
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
num1 = _seedArray[index2];
|
|
50
57
|
}
|
|
51
58
|
for (int index3 = 1; index3 < 5; ++index3)
|
|
52
59
|
{
|
|
53
|
-
for (int index4 = 1; index4 <
|
|
60
|
+
for (int index4 = 1; index4 < SeedArraySize; ++index4)
|
|
54
61
|
{
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
|
|
62
|
+
int value = _seedArray[index4] -= _seedArray[1 + (index4 + 30) % LastSeedIndex];
|
|
63
|
+
if (value < 0)
|
|
64
|
+
{
|
|
65
|
+
_seedArray[index4] += int.MaxValue;
|
|
66
|
+
}
|
|
58
67
|
}
|
|
59
68
|
}
|
|
60
|
-
|
|
61
|
-
|
|
69
|
+
|
|
70
|
+
_inext = 0;
|
|
71
|
+
_inextp = 21;
|
|
62
72
|
}
|
|
63
73
|
|
|
64
74
|
[JsonConstructor]
|
|
@@ -66,59 +76,86 @@
|
|
|
66
76
|
{
|
|
67
77
|
unchecked
|
|
68
78
|
{
|
|
69
|
-
|
|
70
|
-
|
|
79
|
+
_inext = (int)internalState.State1;
|
|
80
|
+
_inextp = (int)internalState.State2;
|
|
71
81
|
}
|
|
72
82
|
_cachedGaussian = internalState.Gaussian;
|
|
73
|
-
|
|
83
|
+
_seedArray = ArrayConverter.ByteArrayToIntArrayBlockCopy(internalState.Payload);
|
|
74
84
|
}
|
|
75
85
|
|
|
76
|
-
public override
|
|
86
|
+
public override int Next()
|
|
77
87
|
{
|
|
78
|
-
int
|
|
79
|
-
int
|
|
88
|
+
int localINext = _inext;
|
|
89
|
+
int localINextP = _inextp;
|
|
80
90
|
int index1;
|
|
81
|
-
if ((index1 =
|
|
91
|
+
if ((index1 = localINext + 1) >= SeedArraySize)
|
|
92
|
+
{
|
|
82
93
|
index1 = 1;
|
|
94
|
+
}
|
|
95
|
+
|
|
83
96
|
int index2;
|
|
84
|
-
if ((index2 =
|
|
97
|
+
if ((index2 = localINextP + 1) >= SeedArraySize)
|
|
98
|
+
{
|
|
85
99
|
index2 = 1;
|
|
86
|
-
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
int num = _seedArray[index1] - _seedArray[index2];
|
|
87
103
|
if (num == int.MaxValue)
|
|
104
|
+
{
|
|
88
105
|
--num;
|
|
106
|
+
}
|
|
107
|
+
|
|
89
108
|
if (num < 0)
|
|
109
|
+
{
|
|
90
110
|
num += int.MaxValue;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
_seedArray[index1] = num;
|
|
114
|
+
_inext = index1;
|
|
115
|
+
_inextp = index2;
|
|
116
|
+
return num;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public override uint NextUint()
|
|
120
|
+
{
|
|
121
|
+
if (NextBool())
|
|
122
|
+
{
|
|
123
|
+
return unchecked((uint)(Next() ^ 0x80000000));
|
|
124
|
+
}
|
|
125
|
+
return unchecked((uint)Next());
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public override bool NextBool()
|
|
129
|
+
{
|
|
130
|
+
return Next() < HalfwayInt;
|
|
95
131
|
}
|
|
96
132
|
|
|
97
133
|
public override double NextDouble()
|
|
98
134
|
{
|
|
99
|
-
double
|
|
135
|
+
double random;
|
|
100
136
|
do
|
|
101
137
|
{
|
|
102
|
-
|
|
103
|
-
} while (
|
|
138
|
+
random = Next() / (1.0 * int.MaxValue);
|
|
139
|
+
} while (1.0 <= random);
|
|
104
140
|
|
|
105
|
-
return
|
|
141
|
+
return random;
|
|
106
142
|
}
|
|
107
143
|
|
|
108
144
|
public override float NextFloat()
|
|
109
145
|
{
|
|
110
|
-
|
|
146
|
+
float random;
|
|
147
|
+
do
|
|
148
|
+
{
|
|
149
|
+
random = Next() * (1f / int.MaxValue);
|
|
150
|
+
} while (1f <= random);
|
|
151
|
+
|
|
152
|
+
return random;
|
|
111
153
|
}
|
|
112
154
|
|
|
113
155
|
public override IRandom Copy()
|
|
114
156
|
{
|
|
115
157
|
SystemRandom copy = new(InternalState);
|
|
116
|
-
|
|
117
|
-
for (int i = 0; i < SeedArray.Length; ++i)
|
|
118
|
-
{
|
|
119
|
-
copy.SeedArray[i] = SeedArray[i];
|
|
120
|
-
}
|
|
121
|
-
|
|
158
|
+
Array.Copy(_seedArray, copy._seedArray, _seedArray.Length);
|
|
122
159
|
return copy;
|
|
123
160
|
}
|
|
124
161
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
namespace UnityHelpers.Tests.Extensions
|
|
2
|
+
{
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using Core.Extension;
|
|
5
|
+
using Core.Random;
|
|
6
|
+
using NUnit.Framework;
|
|
7
|
+
using UnityEngine;
|
|
8
|
+
|
|
9
|
+
public sealed class RandomExtensionTests
|
|
10
|
+
{
|
|
11
|
+
[Test]
|
|
12
|
+
public void NextVector2InRange()
|
|
13
|
+
{
|
|
14
|
+
HashSet<float> seenAngles = new();
|
|
15
|
+
for (int i = 0; i < 1_000; ++i)
|
|
16
|
+
{
|
|
17
|
+
Vector2 vector = PRNG.Instance.NextVector2(-100, 100);
|
|
18
|
+
float range = PRNG.Instance.NextFloat(100f);
|
|
19
|
+
Vector2 inRange = PRNG.Instance.NextVector2InRange(range, vector);
|
|
20
|
+
Assert.LessOrEqual(Vector2.Distance(vector, inRange), range);
|
|
21
|
+
seenAngles.Add(Vector2.SignedAngle(vector, inRange));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
Assert.LessOrEqual(3, seenAngles.Count);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|