-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathrandom.d
More file actions
681 lines (590 loc) · 18.9 KB
/
random.d
File metadata and controls
681 lines (590 loc) · 18.9 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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/++
A random number generator that can work with [std.random] but does not have to.
It is designed to be reasonably good and fast, more for fun than for security.
Authors:
Forked from Herringway's pcg.d file:
https://github.com/Herringway/unexpected/blob/main/pcg/source/unexpected/pcg.d
Modified by Adam D. Ruppe
Copyright:
Original version copyright Herringway, 2023
License: BSL-1.0
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
+/
module arsd.random;
/+
Herringway: adam_d_ruppe: when you get back, there're a few other things you might wanna consider for your std.random
Herringway: like seeding with ranges instead of single values (mersenne twister has a looooot of state that needs seeding, and a single value isn't doing to do a very good job)
Herringway: as well as providing more sources of data to seed with, ike OS APIs n such
+/
// desired functions:
// https://phobos.dpldocs.info/source/std.random.d.html#L2119
/++
Gets a random number from a uniform distribution including min and up to (but not including) max from the reasonable default generator.
History:
Added April 17, 2025
+/
int uniform(int min, int max) {
return uniform(getReasonableDefaultGenerator(), min, max);
}
/// ditto
int uniform(Rng gen, int min, int max) {
auto f = cast(uint) gen.next;
// FIXME i think this is biased but also meh
return f % (max - min) + min;
}
/// ditto
alias randomInteger = uniform;
/+
unittest {
import arsd.core;
writeln(uniform(-10, 0));
}
+/
/++
Gets a random number between 0.0 and 1.0, including 0.0, but not including 1.0.
History:
Added April 18, 2025
+/
float randomFloat() {
return randomFloat(getReasonableDefaultGenerator());
}
/// ditto
float randomFloat(Rng gen) {
auto max = (1 << float.mant_dig) - 1;
float n = uniform(gen, 0, max);
return n / max;
}
// might do a long uniform and maybe double too? idk
/++
Shuffles the contents of the array, in place. Assumes elements can be easily swapped.
(the current implementation is an in-place Fisher-Yates algorithm)
History:
Added April 19, 2025
+/
void shuffle(T)(T[] array) {
shuffle(getReasonableDefaultGenerator(), array);
}
/// ditto
void shuffle(T)(Rng gen, T[] array) {
assert(array.length < int.max);
foreach(index, item; array) {
auto ridx = uniform(gen, cast(int) index, cast(int) array.length);
if(ridx == index)
continue;
array[index] = array[ridx];
array[ridx] = item;
}
}
version(arsd_random_unittest)
unittest {
auto array = [1,2,3,4,5,6,7,8,9,0];
auto results = new int[](array.length);
foreach(i; 0 .. 1_000_000) {
shuffle(array);
auto searchingFor = 9;
foreach(where, item; array)
if(searchingFor == item)
results[where]++;
}
import arsd.core; writeln(results);
}
/++
Returns an index of the weights, with the proportional odds given by the weights.
So weightedChoice([1, 2, 1]) is twice as likely to return 1 as it is 0 or 2.
History:
Added April 19, 2025
+/
int weightedChoice(scope const int[] weights...) {
return weightedChoice(getReasonableDefaultGenerator(), weights);
}
/// ditto
int weightedChoice(Rng gen, scope const int[] weights...) {
int sum = 0;
foreach(weight; weights)
sum += weight;
int val = uniform(gen, 0, sum);
sum = 0;
foreach(idx, weight; weights) {
sum += weight;
if(val < sum)
return cast(int) idx;
}
assert(0);
}
/++
Pick a random number off the normal (aka gaussian) distribution bell curve.
Parameters:
median = median
standardDeviation = standard deviation
min = minimum value to ever return
max = one above the highest value to ever return; an exclusive endpoint
History:
Added April 18, 2025
+/
int bellCurve(int median, int standardDeviation, int min = int.min, int max = int.max) {
return bellCurve(getReasonableDefaultGenerator(), median, standardDeviation, min, max);
}
/// ditto
int bellCurve(Rng gen, int median, int standardDeviation, int min = int.min, int max = int.max) {
import core.stdc.math;
auto mag = standardDeviation * sqrt(-2.0 * log(randomFloat(gen)));
int value = cast(int) (mag * cos(2 * 3.14159268f * randomFloat(gen)) + median);
if(value < min)
value = min;
if(value >= max)
value = max - 1;
return value;
}
// bimodal distribution?
// maybe a pareto distribution too idk tho
version(arsd_random_unittest)
unittest {
int[21] results;
foreach(i; 0 .. 1_000_00) {
//results[uniform(0, cast(int) results.length)] += 1;
//results[bellCurve(10, 3, 0, cast(int) results.length)] += 1;
results[weightedChoice([0, 2, 1, 0, 2, 6, 0, 6, 6])] += 1;
}
import std.stdio; writeln(results);
// foreach(i; 0 .. 10) writeln(bellCurve(100, 10));
}
/++
A simple generic interface to a random number generator.
+/
interface Rng {
/++
Seeds the generator, calling the delegate zero (if it is a true rng), one, or more times to get all the state it needs.
+/
void seed(scope ulong delegate() getEntropy);
/++
Get the next number in the sequence. Some may not actually use all 64 bits of the return type.
+/
ulong next();
/+
/++
Saves a copy of the current generator state to a fresh object.
See_Also:
[saveState], which returns an array of bytes you can save to a file (or whatever)
+/
Rng save() const;
+/
}
/+
interface RestorableRng {
/++
Saves the rng state to an array.
To restore state, you must first construct an object of the same type, then call `restoreState`
on that fresh object. If you get the wrong type, it won't work right (and may or may not throw an exception).
+/
ubyte[] saveState() const;
/// ditto
void restoreState(in ubyte[]);
}
+/
class RngFromRange(R) : Rng {
private R r;
void seed(scope ulong delegate() getEntropy) {
r = R(getEntropy());
}
ulong next() {
auto f = r.front;
r.popFront;
return f;
}
Rng save() const {
auto t = new RngFromRange();
t.r = this.r.save;
return t;
}
}
alias reasonableDefault = PCG!(uint, ulong, xslrr);
/++
Gets a "reasonable default" random number generator, one good enough
for my casual use. This is the object used by the other functions when
you don't explicitly use your own generator.
It will be automatically seeded from the operating system random number
pool if you don't pass one of your own.
History:
Added April 17, 2025
+/
Rng getReasonableDefaultGenerator(lazy ulong seed) @trusted {
static Rng generator;
if(generator is null) {
generator = new RngFromRange!reasonableDefault();
generator.seed(&seed);
}
return generator;
}
/// ditto
Rng getReasonableDefaultGenerator() {
return getReasonableDefaultGenerator(unpredictableSeed());
}
private ulong unpredictableSeed() {
ulong r;
osRandom(r);
return r;
}
version (none) {
} else version (linux) {
private bool osRandom(out ulong result) @trusted {
import core.sys.posix.unistd;
import core.sys.posix.fcntl;
int fd = open("/dev/urandom", O_RDONLY);
if(fd == -1)
return false;
auto ret = read(fd, &result, typeof(result).sizeof);
if(ret != typeof(result).sizeof) {
close(fd);
return false;
}
close(fd);
return true;
}
} else version (Windows) {
pragma(lib, "Bcrypt.lib");
private bool osRandom(out ulong result) @trusted {
import core.sys.windows.windef : PUCHAR, ULONG;
import core.sys.windows.ntdef : NT_SUCCESS;
import core.sys.windows.bcrypt : BCryptGenRandom, BCRYPT_USE_SYSTEM_PREFERRED_RNG;
const gotRandom = BCryptGenRandom(
null,
cast(PUCHAR) &result,
ULONG(typeof(result).sizeof),
BCRYPT_USE_SYSTEM_PREFERRED_RNG,
);
return NT_SUCCESS(gotRandom);
}
} else version (all) {
private bool osRandom(out ulong result) @trusted {
import std.random;
result = std.random.unpredictableSeed;
return false;
}
}
private V rotr(V)(V value, uint r) {
return cast(V)(value >> r | value << (-r & (V.sizeof * 8 - 1)));
}
private int log2(int d) {
assert(__ctfe);
if(d == 8) return 3;
if(d == 16) return 4;
if(d == 32) return 5;
if(d == 64) return 6;
if(d == 128) return 7;
assert(0);
}
struct PCGConsts(X, I) {
enum spareBits = (I.sizeof - X.sizeof) * 8;
enum wantedOpBits = log2(X.sizeof * 8);
struct xshrr {
enum opBits = spareBits >= wantedOpBits ? wantedOpBits : spareBits;
enum amplifier = wantedOpBits - opBits;
enum xShift = (opBits + X.sizeof * 8) / 2;
enum mask = (1 << opBits) - 1;
enum bottomSpare = spareBits - opBits;
}
struct xshrs {
// there must be a simpler way to express this
static if (spareBits - 5 >= 64) {
enum opBits = 5;
} else static if (spareBits - 4 >= 32) {
enum opBits = 4;
} else static if (spareBits - 3 >= 16) {
enum opBits = 3;
} else static if (spareBits - 2 >= 4) {
enum opBits = 2;
} else static if (spareBits - 1 >= 1) {
enum opBits = 1;
} else {
enum opBits = 0;
}
enum xShift = opBits + ((X.sizeof * 8) + mask) / 2;
enum mask = (1 << opBits) - 1;
enum bottomSpare = spareBits - opBits;
}
struct xsh {
enum topSpare = 0;
enum bottomSpare = spareBits - topSpare;
enum xShift = (topSpare + X.sizeof * 8) / 2;
}
struct xsl {
enum topSpare = spareBits;
enum bottomSpare = spareBits - topSpare;
enum xShift = (topSpare + X.sizeof * 8) / 2;
}
struct rxs {
enum shift = (I.sizeof - X.sizeof) * 8;
// there must be a simpler way to express this
static if (shift > 64 + 8) {
enum rShiftAmount = I.sizeof - 6;
enum rShiftMask = 63;
} else static if (shift > 32 + 4) {
enum rShiftAmount = I.sizeof - 5;
enum rShiftMask = 31;
} else static if (shift > 16 + 2) {
enum rShiftAmount = I.sizeof - 4;
enum rShiftMask = 15;
} else static if (shift > 8 + 1) {
enum rShiftAmount = I.sizeof - 3;
enum rShiftMask = 7;
} else static if (shift > 4 + 1) {
enum rShiftAmount = I.sizeof - 2;
enum rShiftMask = 3;
} else static if (shift > 2 + 1) {
enum rShiftAmount = I.sizeof - 1;
enum rShiftMask = 1;
} else {
enum rShiftAmount = 0;
enum rShiftMask = 0;
}
enum extraShift = (X.sizeof - shift)/2;
}
struct rxsm {
enum opBits = log2(X.sizeof * 8) - 1;
enum shift = (I.sizeof - X.sizeof) * 8;
enum mask = (1 << opBits) - 1;
}
struct xslrr {
enum opBits = spareBits >= wantedOpBits ? wantedOpBits : spareBits;
enum amplifier = wantedOpBits - opBits;
enum mask = (1 << opBits) - 1;
enum topSpare = spareBits;
enum bottomSpare = spareBits - topSpare;
enum xShift = (topSpare + X.sizeof * 8) / 2;
}
}
private X xorshift(X, I)(I tmp, uint amt1, uint amt2) {
tmp ^= tmp >> amt1;
return cast(X)(tmp >> amt2);
}
/// XSH RR (xorshift high, random rotate) - decent performance, slightly better results
private X xshrr(X, I)(const I state) {
alias constants = PCGConsts!(X, I).xshrr;
static if (constants.opBits > 0) {
auto rot = (state >> (I.sizeof * 8 - constants.opBits)) & constants.mask;
} else {
enum rot = 0;
}
uint amprot = cast(uint)((rot << constants.amplifier) & constants.mask);
I tmp = state;
return rotr(xorshift!X(tmp, constants.xShift, constants.bottomSpare), amprot);
}
/// XSH RS (xorshift high, random shift) - decent performance
private X xshrs(X, I)(const I state) {
alias constants = PCGConsts!(X, I).xshrs;
static if (constants.opBits > 0) {
uint rshift = (state >> (I.sizeof * 8 - constants.opBits)) & constants.mask;
} else {
uint rshift = 0;
}
I tmp = state;
return xorshift!X(tmp, constants.xShift, cast(uint)(constants.bottomSpare - constants.mask + rshift));
}
/// XSH (fixed xorshift, high) - don't use this for anything smaller than ulong
private X xsh(X, I)(const I state) {
alias constants = PCGConsts!(X, I).xsh;
I tmp = state;
return xorshift!X(tmp, constants.xShift, constants.bottomSpare);
}
/// XSL (fixed xorshift, low) - don't use this for anything smaller than ulong
private X xsl(X, I)(const I state) {
alias constants = PCGConsts!(X, I).xsl;
I tmp = state;
return xorshift!X(tmp, constants.xShift, constants.bottomSpare);
}
/// RXS (random xorshift)
private X rxs(X, I)(const I state) {
alias constants = PCGConsts!(X, I).rxs;
uint rshift = (state >> constants.rShiftAmount) & constants.rShiftMask;
I tmp = state;
return xorshift!X(tmp, cast(uint)(constants.shift + constants.extraShift - rshift), rshift);
}
/++
RXS M XS (random xorshift, multiply, fixed xorshift)
This has better statistical properties, but supposedly performs worse. This
was not reproducible, however.
+/
private X rxsmxs(X, I)(const I state) {
X result = rxsm!X(state);
result ^= result >> ((2 * X.sizeof * 8 + 2) / 3);
return result;
}
/// RXS M (random xorshift, multiply)
private X rxsm(X, I)(const I state) {
alias constants = PCGConsts!(X, I).rxsm;
I tmp = state;
static if (constants.opBits > 0) {
uint rshift = (tmp >> (I.sizeof * 8 - constants.opBits)) & constants.mask;
} else {
uint rshift = 0;
}
tmp ^= tmp >> (constants.opBits + rshift);
tmp *= PCGMMultiplier!I;
return cast(X)(tmp >> constants.shift);
}
/// DXSM (double xorshift, multiply) - newer, better performance for types 2x the size of the largest type the cpu can handle
private X dxsm(X, I)(const I state) {
static assert(X.sizeof <= I.sizeof/2, "Output type must be half the size of the state type.");
X hi = cast(X)(state >> ((I.sizeof - X.sizeof) * 8));
X lo = cast(X)state;
lo |= 1;
hi ^= hi >> (X.sizeof * 8 / 2);
hi *= PCGMMultiplier!I;
hi ^= hi >> (3*(X.sizeof * 8 / 4));
hi *= lo;
return hi;
}
/// XSL RR (fixed xorshift, random rotate) - better performance for types 2x the size of the largest type the cpu can handle
private X xslrr(X, I)(const I state) {
alias constants = PCGConsts!(X, I).xslrr;
I tmp = state;
static if (constants.opBits > 0) {
uint rot = (tmp >> (I.sizeof * 8 - constants.opBits)) & constants.mask;
} else {
uint rot = 0;
}
uint amprot = (rot << constants.amplifier) & constants.mask;
return rotr(xorshift!X(tmp, constants.xShift, constants.bottomSpare), amprot);
}
struct PCG(T, S, alias func, S multiplier = DefaultPCGMultiplier!S, S increment = DefaultPCGIncrement!S) {
private S state;
this(S val) @safe pure nothrow @nogc {
seed(val);
}
void seed(S val) @safe pure nothrow @nogc {
state = cast(S)(val + increment);
popFront();
}
void popFront() @safe pure nothrow @nogc {
state = cast(S)(state * multiplier + increment);
}
T front() const @safe pure nothrow @nogc @property {
return func!T(state);
}
typeof(this) save() @safe pure nothrow @nogc const {
return this;
}
enum bool empty = false;
enum bool isUniformRandom = true;
enum T min = T.min;
enum T max = T.max;
const(S) toSiryulType()() const @safe {
return state;
}
static PCG fromSiryulType()(S val) @safe {
PCG result;
result.state = val;
return result;
}
}
template DefaultPCGMultiplier(T) {
static if (is(T == ubyte)) {
enum DefaultPCGMultiplier = 141;
} else static if (is(T == ushort)) {
enum DefaultPCGMultiplier = 12829;
} else static if (is(T == uint)) {
enum DefaultPCGMultiplier = 747796405;
} else static if (is(T == ulong)) {
enum DefaultPCGMultiplier = 6364136223846793005;
} else static if (is(T == ucent)) {
//enum DefaultPCGMultiplier = 47026247687942121848144207491837523525;
}
}
template DefaultPCGIncrement(T) {
static if (is(T == ubyte)) {
enum DefaultPCGIncrement = 77;
} else static if (is(T == ushort)) {
enum DefaultPCGIncrement = 47989;
} else static if (is(T == uint)) {
enum DefaultPCGIncrement = 2891336453;
} else static if (is(T == ulong)) {
enum DefaultPCGIncrement = 1442695040888963407;
} else static if (is(T == ucent)) {
//enum DefaultPCGIncrement = 117397592171526113268558934119004209487;
}
}
private template PCGMMultiplier(T) {
static if (is(T : ubyte)) {
enum PCGMMultiplier = 217;
} else static if (is(T : ushort)) {
enum PCGMMultiplier = 62169;
} else static if (is(T : uint)) {
enum PCGMMultiplier = 277803737;
} else static if (is(T : ulong)) {
enum PCGMMultiplier = 12605985483714917081;
//} else static if (is(T == ucent)) {
//enum PCGMMultiplier = 327738287884841127335028083622016905945;
}
}
version(arsd_random_unittest) {
private alias AliasSeq(T...) = T;
alias SupportedTypes = AliasSeq!(ubyte, ushort, uint, ulong);
alias SupportedFunctions = AliasSeq!(xshrr, xshrs, xsh, xsl, rxs, rxsmxs, rxsm, xslrr);
static foreach (ResultType; SupportedTypes) {
static foreach (StateType; SupportedTypes) {
static if (StateType.sizeof >= ResultType.sizeof) {
static foreach (Function; SupportedFunctions) {
mixin("alias PCG", int(StateType.sizeof * 8), int(ResultType.sizeof * 8), __traits(identifier, Function), " = PCG!(ResultType, StateType, Function);");
}
}
}
}
alias PCG6432dxsm = PCG!(uint, ulong, dxsm);
@safe unittest {
import std.algorithm : reduce;
import std.datetime.stopwatch : benchmark;
import std.math : pow, sqrt;
import std.random : isSeedable, Mt19937, uniform, uniform01, unpredictableSeed;
import std.stdio : writefln, writeln;
auto seed = unpredictableSeed;
void testRNG(RNG, string name)(uint seed) {
static if (isSeedable!(RNG, uint)) {
auto rng = RNG(seed);
} else static if (isSeedable!(RNG, ushort)) {
auto rng = RNG(cast(ushort)seed);
} else static if (isSeedable!(RNG, ubyte)) {
auto rng = RNG(cast(ubyte)seed);
}
writefln!"--%s--"(name);
double total = 0;
ulong[ubyte] distribution;
void test() {
total += uniform01(rng);
distribution.require(uniform!ubyte(rng), 0)++;
}
auto result = benchmark!(test)(1000000)[0];
writefln!"Benchmark completed in %s"(result);
writeln(total);
double avg = reduce!((a, b) => a + b / distribution.length)(0.0f, distribution);
auto var = reduce!((a, b) => a + pow(b - avg, 2) / distribution.length)(0.0f, distribution);
auto sd = sqrt(var);
writefln!"Average: %s, Standard deviation: %s"(avg, sd);
}
testRNG!(PCG168xshrr, "PCG168xshrr")(seed);
testRNG!(PCG3216xshrr, "PCG3216xshrr")(seed);
testRNG!(PCG6432xslrr, "PCG6432xslrr")(seed);
testRNG!(PCG648rxsmxs, "PCG648rxsmxs")(seed);
testRNG!(PCG6432dxsm, "PCG6432dxsm")(seed);
testRNG!(Mt19937, "Mt19937")(seed);
testRNG!(reasonableDefault, "reasonableDefault")(seed);
}
}