-
Notifications
You must be signed in to change notification settings - Fork 1
/
carandom.pas
1047 lines (912 loc) · 29.9 KB
/
carandom.pas
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit caRandom;
{$INCLUDE ca.inc}
interface
uses
// Standard Delphi units
SysUtils,
Windows,
// ca units
caClasses,
caUtils;
const
cAdditiveRandomSize = 55;
cShuffleRandomSize = 97;
cCombinedMaxInt1 = MaxInt - 84;
cCombinedMaxInt2 = MaxInt - 248;
type
PcaRandomTable = ^TcaRandomTable;
TcaRandomTable = array[0..0] of Double;
TcaRandomizeType = (rtSystem, rtMinStandard, rtCombined, rtAdditive, rtShuffle);
//---------------------------------------------------------------------------
// IcaRandom
//---------------------------------------------------------------------------
IcaRandom = interface
['{31542E2B-B33F-4AEB-A273-8DDF444B24BB}']
// Property methods
function GetAsDouble: Double;
function GetAsInteger: Integer;
function GetLowerBound: Double;
function GetLowerIntBound: Integer;
function GetRandomizeType: TcaRandomizeType;
function GetSeed: Integer;
function GetUpperBound: Double;
function GetUpperIntBound: Integer;
procedure SetLowerBound(const Value: Double);
procedure SetLowerIntBound(const Value: Integer);
procedure SetSeed(const AValue: Integer);
procedure SetUpperBound(const Value: Double);
procedure SetUpperIntBound(const Value: Integer);
// Properties
property AsDouble: Double read GetAsDouble;
property AsInteger: Integer read GetAsInteger;
property Seed: Integer read GetSeed write SetSeed;
property LowerBound: Double read GetLowerBound write SetLowerBound;
property LowerIntBound: Integer read GetLowerIntBound write SetLowerIntBound;
property RandomizeType: TcaRandomizeType read GetRandomizeType;
property UpperBound: Double read GetUpperBound write SetUpperBound;
property UpperIntBound: Integer read GetUpperIntBound write SetUpperIntBound;
end;
//---------------------------------------------------------------------------
// TcaBaseRandom
//---------------------------------------------------------------------------
TcaBaseRandom = class(TcaInterfacedPersistent, IcaRandom)
private
// Property fields
FLowerBound: Double;
FLowerIntBound: Integer;
FMathUtils: IcaMathUtils;
FRandomizeType: TcaRandomizeType;
FSeed: Integer;
FUpperBound: Double;
FUpperIntBound: Integer;
// Property methods
function GetAsDouble: Double;
function GetAsInteger: Integer;
function GetLowerBound: Double;
function GetLowerIntBound: Integer;
function GetRandomizeType: TcaRandomizeType;
function GetUpperBound: Double;
function GetUpperIntBound: Integer;
procedure SetLowerBound(const Value: Double);
procedure SetLowerIntBound(const Value: Integer);
procedure SetUpperBound(const Value: Double);
procedure SetUpperIntBound(const Value: Integer);
protected
// Protected property methods
function GetSeed: Integer; virtual;
procedure SetSeed(const AValue: Integer); virtual;
// Protected static methods
function GetAdjustedSeed(ASeed: Integer; AMaxInt: Integer = MaxInt): Integer;
procedure SetRandomizeType(ARandomizeType: TcaRandomizeType);
// Protected virtual methods
procedure DoAdjustSeed; virtual;
procedure DoGetDouble(var ADouble: Double); virtual; abstract;
// Protected properties
property Seed: Integer read GetSeed write SetSeed;
public
constructor Create(ASeed: Integer = 0); virtual;
// Properties
property AsDouble: Double read GetAsDouble;
property AsInteger: Integer read GetAsInteger;
property LowerBound: Double read GetLowerBound write SetLowerBound;
property LowerIntBound: Integer read GetLowerIntBound write SetLowerIntBound;
property RandomizeType: TcaRandomizeType read GetRandomizeType;
property UpperBound: Double read GetUpperBound write SetUpperBound;
property UpperIntBound: Integer read GetUpperIntBound write SetUpperIntBound;
end;
//---------------------------------------------------------------------------
// TcaMinStandardRandom
//---------------------------------------------------------------------------
TcaMinStandardRandom = class(TcaBaseRandom)
protected
// Protected virtual methods
procedure DoGetDouble(var ADouble: Double); override;
public
// Virtual public methods
procedure AfterConstruction; override;
// Promoted properties
property Seed;
end;
//---------------------------------------------------------------------------
// IcaSystemRandom
//---------------------------------------------------------------------------
IcaSystemRandom = interface
['{2C2A7FDA-3837-4148-8F39-74BB2DE8A276}']
// Public methods
procedure Randomize;
end;
//---------------------------------------------------------------------------
// TcaSystemRandom
//---------------------------------------------------------------------------
TcaSystemRandom = class(TcaBaseRandom, IcaSystemRandom)
protected
// Protected virtual methods
procedure DoAdjustSeed; override;
procedure DoGetDouble(var ADouble: Double); override;
public
// Virtual public methods
procedure AfterConstruction; override;
// Public methods
procedure Randomize;
// Promoted properties
property Seed;
end;
//---------------------------------------------------------------------------
// IcaCombinedRandom
//---------------------------------------------------------------------------
IcaCombinedRandom = interface
['{AA27D4CA-F5AB-4003-87B8-41F63E26EE45}']
// Property methods
function GetSeed1: Integer;
function GetSeed2: Integer;
procedure SetSeed1(const Value: Integer);
procedure SetSeed2(const Value: Integer);
// Properties
property Seed1: Integer read GetSeed1 write SetSeed1;
property Seed2: Integer read GetSeed2 write SetSeed2;
end;
//---------------------------------------------------------------------------
// TcaCombinedRandom
//---------------------------------------------------------------------------
TcaCombinedRandom = class(TcaBaseRandom, IcaCombinedRandom)
private
FSeed1: Integer;
FSeed2: Integer;
// Property methods
function GetSeed1: Integer;
function GetSeed2: Integer;
procedure SetSeed1(const Value: Integer);
procedure SetSeed2(const Value: Integer);
protected
// Protected virtual methods
procedure DoGetDouble(var ADouble: Double); override;
public
constructor Create(ASeed1: Integer = 0; ASeed2: Integer = 0); reintroduce;
// Virtual public methods
procedure AfterConstruction; override;
// Properties
property Seed1: Integer read GetSeed1 write SetSeed1;
property Seed2: Integer read GetSeed2 write SetSeed2;
end;
//---------------------------------------------------------------------------
// TcaDelegatedRandom
//---------------------------------------------------------------------------
TcaDelegatedRandom = class(TcaBaseRandom)
private
// Property fields
FStdRandom: TcaMinStandardRandom;
FTable: PcaRandomTable;
protected
// Protected property methods
function GetSeed: Integer; override;
procedure SetSeed(const AValue: Integer); override;
// Protected virtual methods
function GetTableSize: Integer; virtual; abstract;
procedure DoInitializeTable; virtual;
// Protected properties
property StdRandom: TcaMinStandardRandom read FStdRandom;
property Table: PcaRandomTable read FTable;
public
constructor Create(ASeed: Integer = 0); override;
destructor Destroy; override;
end;
//---------------------------------------------------------------------------
// TcaAdditiveRandom
//---------------------------------------------------------------------------
TcaAdditiveRandom = class(TcaDelegatedRandom)
private
// Private fields
FIndex1: Integer;
FIndex2: Integer;
protected
// Protected virtual methods
function GetTableSize: Integer; override;
procedure DoGetDouble(var ADouble: Double); override;
public
constructor Create(ASeed: Integer = 0); override;
// Virtual public methods
procedure AfterConstruction; override;
// Promoted properties
property Seed;
end;
//---------------------------------------------------------------------------
// TcaShuffleRandom
//---------------------------------------------------------------------------
TcaShuffleRandom = class(TcaDelegatedRandom)
private
FAux: Double;
protected
// Protected virtual methods
function GetTableSize: Integer; override;
procedure DoGetDouble(var ADouble: Double); override;
procedure DoInitializeTable; override;
public
// Virtual public methods
procedure AfterConstruction; override;
// Promoted properties
property Seed;
end;
//---------------------------------------------------------------------------
// IcaRandomTest
//---------------------------------------------------------------------------
TcaRandomTestType = (ttCoupon, ttGap, ttPoker, ttUniformity);
IcaRandomTest = interface
['{53267511-E7B7-4DDF-9354-0DE600604D58}']
// Property methods
function GetChiScore: Double;
function GetCurrentTestType: TcaRandomTestType;
function GetDegsFreedom: Integer;
function GetRandom: IcaRandom;
procedure SetRandom(const Value: IcaRandom);
// Properties
property ChiScore: Double read GetChiScore;
property DegsFreedom: Integer read GetDegsFreedom;
property CurrentTestType: TcaRandomTestType read GetCurrentTestType;
property Random: IcaRandom read GetRandom write SetRandom;
end;
//---------------------------------------------------------------------------
// IcaRandomCouponCollectorsTest
//---------------------------------------------------------------------------
IcaRandomCouponCollectorsTest = interface(IcaRandomTest)
['{66159770-ADAA-4FB7-9130-7F4634961814}']
// Public methods
procedure Run;
end;
//---------------------------------------------------------------------------
// IcaRandomGapTest
//---------------------------------------------------------------------------
IcaRandomGapTest = interface(IcaRandomTest)
['{1818C8EA-3C90-4066-903C-4BEE709AA1E5}']
// Property methods
function GetLowerBound: Double;
function GetUpperBound: Double;
procedure SetLowerBound(const Value: Double);
procedure SetUpperBound(const Value: Double);
// Public methods
procedure Run;
// Properties
property LowerBound: Double read GetLowerBound write SetLowerBound;
property UpperBound: Double read GetUpperBound write SetUpperBound;
end;
//---------------------------------------------------------------------------
// IcaRandomPokerTest
//---------------------------------------------------------------------------
IcaRandomPokerTest = interface(IcaRandomTest)
['{44F7F081-199B-4480-AAAC-2F2E88C94512}']
// Public methods
procedure Run;
end;
//---------------------------------------------------------------------------
// IcaRandomUniformityTest
//---------------------------------------------------------------------------
IcaRandomUniformityTest = interface(IcaRandomTest)
['{73025B1E-CCFD-4074-9A9B-4AE3E38A4C19}']
// Public methods
procedure Run;
end;
//---------------------------------------------------------------------------
// TcaRandomTest
//---------------------------------------------------------------------------
TcaRandomTest = class(TcaInterfacedPersistent, IcaRandomTest,
IcaRandomCouponCollectorsTest,
IcaRandomGapTest,
IcaRandomPokerTest,
IcaRandomUniformityTest)
private
// Private fields
FMathUtils: IcaMathUtils;
// Property fields
FChiScore: Double;
FCurrentTestType: TcaRandomTestType;
FDegsFreedom: Integer;
FLowerBound: Double;
FRandom: IcaRandom;
FUpperBound: Double;
// Private methods
procedure RunCouponCollectorsTest;
procedure RunGapTest;
procedure RunPokerTest;
procedure RunUniformityTest;
// Property methods
function GetChiScore: Double;
function GetCurrentTestType: TcaRandomTestType;
function GetDegsFreedom: Integer;
function GetLowerBound: Double;
function GetRandom: IcaRandom;
function GetUpperBound: Double;
procedure SetLowerBound(const Value: Double);
procedure SetRandom(const Value: IcaRandom);
procedure SetUpperBound(const Value: Double);
public
// Virtual public methods
procedure AfterConstruction; override;
// Method resolution clauses
procedure IcaRandomCouponCollectorsTest.Run = RunCouponCollectorsTest;
procedure IcaRandomGapTest.Run = RunGapTest;
procedure IcaRandomPokerTest.Run = RunPokerTest;
procedure IcaRandomUniformityTest.Run = RunUniformityTest;
// Properties
property ChiScore: Double read GetChiScore;
property CurrentTestType: TcaRandomTestType read GetCurrentTestType;
property DegsFreedom: Integer read GetDegsFreedom;
property LowerBound: Double read GetLowerBound write SetLowerBound;
property Random: IcaRandom read GetRandom write SetRandom;
property UpperBound: Double read GetUpperBound write SetUpperBound;
end;
implementation
//---------------------------------------------------------------------------
// TcaBaseRandom
//---------------------------------------------------------------------------
constructor TcaBaseRandom.Create(ASeed: Integer = 0);
begin
inherited Create;
FMathUtils := Utils as IcaMathUtils;
FSeed := ASeed;
DoAdjustSeed;
end;
// Protected virtual methods
procedure TcaBaseRandom.DoAdjustSeed;
begin
FSeed := GetAdjustedSeed(FSeed);
end;
// Protected static methods
function TcaBaseRandom.GetAdjustedSeed(ASeed: Integer; AMaxInt: Integer = MaxInt): Integer;
begin
if ASeed > 0 then
Result := ASeed
else
Result := Integer(GetTickCount);
while Result >= (AMaxInt - 1) do
Result := Result - AMaxInt;
end;
procedure TcaBaseRandom.SetRandomizeType(ARandomizeType: TcaRandomizeType);
begin
FRandomizeType := ARandomizeType;
end;
// Protected property methods
function TcaBaseRandom.GetSeed: Integer;
begin
Result := FSeed;
end;
procedure TcaBaseRandom.SetSeed(const AValue: Integer);
begin
FSeed := AValue;
DoAdjustSeed;
end;
// Property methods
function TcaBaseRandom.GetAsDouble: Double;
begin
DoGetDouble(Result);
if (FLowerBound <> 0) or (FUpperBound <> 0) then
Result := (Result * (FUpperBound - FLowerBound)) + FLowerBound;
end;
function TcaBaseRandom.GetAsInteger: Integer;
var
ADouble: Double;
LowerInt: Integer;
UpperInt: Integer;
begin
DoGetDouble(ADouble);
if (FLowerIntBound <> 0) or (FUpperIntBound <> 0) then
begin
LowerInt := FLowerIntBound + 1;
UpperInt := FUpperIntBound + 2;
ADouble := (ADouble * (UpperInt - LowerInt)) + LowerInt;
end;
Result := FMathUtils.Trunc(ADouble) + 1;
if Result > FUpperIntBound then
Result := FLowerIntBound + Ord(Odd(Result));
if Result > FUpperIntBound then
Dec(Result);
end;
function TcaBaseRandom.GetLowerBound: Double;
begin
Result := FLowerBound;
end;
function TcaBaseRandom.GetLowerIntBound: Integer;
begin
Result := FLowerIntBound;
end;
function TcaBaseRandom.GetRandomizeType: TcaRandomizeType;
begin
Result := FRandomizeType;
end;
function TcaBaseRandom.GetUpperBound: Double;
begin
Result := FUpperBound;
end;
function TcaBaseRandom.GetUpperIntBound: Integer;
begin
Result := FUpperIntBound;
end;
procedure TcaBaseRandom.SetLowerBound(const Value: Double);
begin
FLowerBound := Value;
end;
procedure TcaBaseRandom.SetLowerIntBound(const Value: Integer);
begin
FLowerIntBound := Value;
end;
procedure TcaBaseRandom.SetUpperBound(const Value: Double);
begin
FUpperBound := Value;
end;
procedure TcaBaseRandom.SetUpperIntBound(const Value: Integer);
begin
FUpperIntBound := Value;
end;
//---------------------------------------------------------------------------
// TcaMinStandardRandom
//---------------------------------------------------------------------------
// Virtual public methods
procedure TcaMinStandardRandom.AfterConstruction;
begin
inherited;
SetRandomizeType(rtMinStandard);
end;
// Protected methods
procedure TcaMinStandardRandom.DoGetDouble(var ADouble: Double);
const
cA = 16807;
cQ = MaxInt div cA;
cR = MaxInt mod cA;
cOneOverM = 1 / MaxInt;
var
K: Integer;
ASeed: Integer;
begin
ASeed := Seed;
K := ASeed div cQ;
ASeed := (cA * (ASeed - (K * cQ))) - (K * cR);
if (ASeed < 0) then Inc(ASeed, MaxInt);
ADouble := ASeed * cOneOverM;
Seed := ASeed;
end;
//---------------------------------------------------------------------------
// TcaSystemRandom
//---------------------------------------------------------------------------
// Public methods
procedure TcaSystemRandom.Randomize;
begin
Seed := Integer(GetTickCount);
end;
// Virtual public methods
procedure TcaSystemRandom.AfterConstruction;
begin
inherited;
SetRandomizeType(rtSystem);
end;
// Protected virtual methods
procedure TcaSystemRandom.DoAdjustSeed;
begin
// Bypass ancestor DoAdjustSeed methods
end;
procedure TcaSystemRandom.DoGetDouble(var ADouble: Double);
var
SavedSeed: Integer;
begin
SavedSeed := System.RandSeed;
System.RandSeed := Seed;
ADouble := System.Random;
Seed := System.RandSeed;
System.RandSeed := SavedSeed;
end;
//---------------------------------------------------------------------------
// TcaCombinedRandom
//---------------------------------------------------------------------------
constructor TcaCombinedRandom.Create(ASeed1: Integer = 0; ASeed2: Integer = 0);
begin
inherited Create(0);
SetSeed1(ASeed1);
SetSeed2(ASeed2);
end;
// Virtual public methods
procedure TcaCombinedRandom.AfterConstruction;
begin
inherited;
SetRandomizeType(rtCombined);
end;
// Protected virtual methods
procedure TcaCombinedRandom.DoGetDouble(var ADouble: Double);
const
cA1 = 40014;
cQ1 = cCombinedMaxInt1 div cA1;
cR1 = cCombinedMaxInt1 mod cA1;
cOneOverM1 = 1 / cCombinedMaxInt1;
cA2 = 40692;
cQ2 = cCombinedMaxInt2 div cA2;
cR2 = cCombinedMaxInt2 mod cA2;
var
K: Integer;
SeedDiff: Integer;
begin
// Advance first generator
K := FSeed1 div cQ1;
FSeed1 := (cA1 * (FSeed1 - (K * cQ1))) - (K * cR1);
if (FSeed1 < 0) then Inc(FSeed1, cCombinedMaxInt1);
// Advance second generator
K := FSeed2 div cQ2;
FSeed2 := (cA2 * (FSeed2 - (K * cQ2))) - (K * cR2);
if (FSeed2 < 0) then Inc(FSeed2, cCombinedMaxInt2);
// Combine the two seeds
SeedDiff := FSeed1 - FSeed2;
if (SeedDiff <= 0) then
SeedDiff := SeedDiff + cCombinedMaxInt1 - 1;
ADouble := SeedDiff * cOneOverM1;
end;
// Property methods
function TcaCombinedRandom.GetSeed1: Integer;
begin
Result := FSeed1;
end;
function TcaCombinedRandom.GetSeed2: Integer;
begin
Result := FSeed2;
end;
procedure TcaCombinedRandom.SetSeed1(const Value: Integer);
begin
FSeed1 := GetAdjustedSeed(Value, cCombinedMaxInt1);
end;
procedure TcaCombinedRandom.SetSeed2(const Value: Integer);
begin
FSeed2 := GetAdjustedSeed(Value, cCombinedMaxInt2);
end;
//---------------------------------------------------------------------------
// TcaDelegatedRandom
//---------------------------------------------------------------------------
constructor TcaDelegatedRandom.Create(ASeed: Integer = 0);
begin
inherited Create(ASeed);
GetMem(FTable, GetTableSize * SizeOf(Double));
FStdRandom := TcaMinStandardRandom.Create(ASeed);
DoInitializeTable;
end;
destructor TcaDelegatedRandom.Destroy;
begin
FStdRandom.Free;
FreeMem(FTable);
inherited;
end;
procedure TcaDelegatedRandom.DoInitializeTable;
var
Index: Integer;
begin
for Index := Pred(GetTableSize) downto 0 do
FTable^[Index] := StdRandom.AsDouble;
end;
// Protected property methods
function TcaDelegatedRandom.GetSeed: Integer;
begin
Result := FStdRandom.Seed;
end;
procedure TcaDelegatedRandom.SetSeed(const AValue: Integer);
begin
FStdRandom.Seed := AValue;
DoInitializeTable;
end;
//---------------------------------------------------------------------------
// TcaAdditiveRandom
//---------------------------------------------------------------------------
constructor TcaAdditiveRandom.Create(ASeed: Integer = 0);
begin
inherited Create(ASeed);
FIndex1 := Pred(cAdditiveRandomSize);
FIndex2 := 23;
end;
// Virtual public methods
procedure TcaAdditiveRandom.AfterConstruction;
begin
inherited;
SetRandomizeType(rtAdditive);
end;
// Protected virtual methods
function TcaAdditiveRandom.GetTableSize: Integer;
begin
Result := cAdditiveRandomSize;
end;
procedure TcaAdditiveRandom.DoGetDouble(var ADouble: Double);
begin
ADouble := FTable[FIndex1] + FTable[FIndex2];
if ADouble >= 1.0 then ADouble := ADouble - 1.0;
FTable[FIndex1] := ADouble;
Inc(FIndex1);
if FIndex1 >= cAdditiveRandomSize then FIndex1 := 0;
Inc(FIndex2);
if FIndex2 >= cAdditiveRandomSize then FIndex2 := 0;
end;
//---------------------------------------------------------------------------
// TcaShuffleRandom
//---------------------------------------------------------------------------
// Virtual public methods
procedure TcaShuffleRandom.AfterConstruction;
begin
inherited;
SetRandomizeType(rtShuffle);
end;
// Protected virtual methods
function TcaShuffleRandom.GetTableSize: Integer;
begin
Result := cShuffleRandomSize;
end;
procedure TcaShuffleRandom.DoGetDouble(var ADouble: Double);
var
Index: Integer;
begin
Index := FMathUtils.Trunc(FAux * cShuffleRandomSize);
ADouble := FTable[Index];
FAux := ADouble;
FTable[Index] := StdRandom.AsDouble;
end;
procedure TcaShuffleRandom.DoInitializeTable;
begin
inherited;
FAux := StdRandom.AsDouble;
end;
//---------------------------------------------------------------------------
// TcaRandomTest
//---------------------------------------------------------------------------
const
cUniformityCount = 30000;
cUniformityIntervals = 100;
cGapBucketCount = 10;
cGapsCount = 30000;
cPokerCount = 30000;
cCouponCount = 30000;
// Public methods
// The coupon collectors test
//---------------------------
// The random numbers are read one by one, converted into a number from
// 0 to 4. The length of the sequence required to get a complete set of
// the digits 0..4 is counted, this will vary from 5 upwards. Once a full
// set is obtained, start over. Bucket the lengths of these sequences.
// Apply Chi-Squared test to the buckets.
// Virtual public methods
procedure TcaRandomTest.AfterConstruction;
begin
inherited;
FMathUtils := Utils as IcaMathUtils;
end;
// Private methods
procedure TcaRandomTest.RunCouponCollectorsTest;
var
Bucket: array [5..20] of Integer;
ChiSqVal: Double;
Expected: Double;
Index: Integer;
LenSeq: Integer;
NewVal: Integer;
NumSeqs: Integer;
NumVals: Integer;
Occurs: array [0..4] of Boolean;
Probs: array [5..20] of Double;
begin
// Calculate probabilities for each bucket, algorithm from Knuth
Probs[20] := 1.0;
for Index := 5 to 19 do
begin
Probs[Index] := (120.0 * FMathUtils.Stirling2(Index - 1, 4)) / FMathUtils.IntPower(5.0, Index);
Probs[20] := Probs[20] - Probs[Index];
end;
// an alternative to calculate the last probability value:
// Probs[last] := 1.0 - ((120.0 * Stirling(Last - 1, 5)) / IntPower(5.0, Last - 1));
NumSeqs := 0;
FillChar(Bucket, SizeOf(Bucket), 0);
while (NumSeqs < cCouponCount) do
begin
// keep getting coupons (ie random numbers) until we have collected all five
LenSeq := 0;
NumVals := 0;
FillChar(Occurs, SizeOf(Occurs), 0);
repeat
Inc(LenSeq);
NewVal := FMathUtils.Trunc(FRandom.AsDouble * 5);
if not Occurs[NewVal] then
begin
Occurs[NewVal] := True;
Inc(NumVals);
end;
until NumVals = 5;
// update the relevant bucket depending on the number of coupons we had to collect
if LenSeq > 20 then LenSeq := 20;
Inc(Bucket[LenSeq]);
Inc(NumSeqs);
end;
// Calculate ChiSquare value}
ChiSqVal := 0.0;
for Index := 5 to 20 do
begin
Expected := Probs[Index] * NumSeqs;
ChiSqVal := ChiSqVal + (FMathUtils.Sqr(Expected - Bucket[Index]) / Expected);
end;
// Return results
FChiScore := ChiSqVal;
FDegsFreedom := 15;
FCurrentTestType := ttCoupon;
end;
// The gap test
//-------------
// Each random number is tested to be in the range Lower..Upper. If it
// is a value of 1 is assigned, if not 0 is assigned. You'll get a stream
// of 0's and 1's. The lengths of the runs of 0's are then counted. These
// lengths are then bucketed, you'll get lengths of 0 upwards. These
// lengths are the 'gaps' between 1's. Apply Chi-Squared test to the
// buckets.
procedure TcaRandomTest.RunGapTest;
var
BoundDiff: Double;
Bucket: array [0..Pred(cGapBucketCount)] of integer;
ChiSqVal: Double;
Expected: Double;
GapLen: Integer;
Index: Integer;
NumGaps: Integer;
RandValue: Double;
begin
// Calculate gaps and fill buckets
FillChar(Bucket, SizeOf(Bucket), 0);
GapLen := 0;
NumGaps := 0;
while (NumGaps < cGapsCount) do
begin
RandValue := FRandom.AsDouble;
if (FLowerBound <= RandValue) and (RandValue < FUpperBound) then
begin
if (GapLen >= cGapBucketCount) then
GapLen := Pred(cGapBucketCount);
Inc(Bucket[GapLen]);
Inc(NumGaps);
GapLen := 0;
end
else
begin
if (GapLen < cGapBucketCount) then
Inc(GapLen);
end;
end;
BoundDiff := FUpperBound - FLowerBound;
ChiSqVal := 0.0;
// Do all but the last bucket
for Index := 0 to cGapBucketCount - 2 do
begin
Expected := BoundDiff * FMathUtils.IntPower(1 - BoundDiff, Index) * NumGaps;
ChiSqVal := ChiSqVal + (FMathUtils.Sqr(Expected - Bucket[Index]) / Expected);
end;
// Do the last bucket}
Index := Pred(cGapBucketCount);
Expected := FMathUtils.IntPower(1 - BoundDiff, Index) * NumGaps;
ChiSqVal := ChiSqVal + (FMathUtils.Sqr(Expected - Bucket[Index]) / Expected);
// Return results
FChiScore := ChiSqVal;
FDegsFreedom := Pred(cGapBucketCount);
FCurrentTestType := ttGap;
end;
// The poker test
//---------------
// The random numbers are grouped into 'hands' of 5, and the numbers are
// converted into a digit from 0..9. The number of different digits in
// each hand is then counted (1..5), and this result is bucketed. Because
// the probability of only one digit repeated 5 times is so low, it is
// grouped into the 2-different-digit category. Apply Chi-Squared test to
// the buckets.
procedure TcaRandomTest.RunPokerTest;
var
Accum: Double;
Bucket: array [0..4] of Integer;
BucketNumber: Integer;
ChiSqVal: Double;
Divisor: Double;
Expected: Double;
Flag: array [0..9] of Boolean;
FlagIndex: Integer;
Index: Integer;
NumFives: Integer;
Probs: array [0..4] of Double;
begin
// Prepare
FillChar(Bucket, SizeOf(Bucket), 0);
NumFives := cPokerCount div 5;
// Calculate probabilities for each bucket, algorithm from Knuth
Accum := 1.0;
Divisor := FMathUtils.IntPower(10.0, 5);
for Index := 0 to 4 do
begin
Accum := Accum * (10.0 - Index);
Probs[Index] := Accum * FMathUtils.Stirling2(5, Succ(Index)) / Divisor;
end;
// For each group of five random numbers, convert all five to a
// number between 1 and 10, count the number of different digits
for Index := 1 to NumFives do
begin
FillChar(Flag, SizeOf(Flag), 0);
for FlagIndex := 1 to 5 do
Flag[FMathUtils.Trunc(FRandom.AsDouble * 10.0)] := True;
BucketNumber := -1;
for FlagIndex := 0 to 9 do
if Flag[FlagIndex] then Inc(BucketNumber);
Inc(Bucket[BucketNumber]);
end;
// Accumulate the first bucket into the second, do calc separately -
// it'll be the sum of the 'all the same' and 'two different digits' buckets
Inc(Bucket[1], Bucket[0]);
Expected := (Probs[0] + Probs[1]) * NumFives;
ChiSqVal := FMathUtils.Sqr(Expected - Bucket[1]) / Expected;
// Write the other buckets
for Index := 2 to 4 do
begin
Expected := Probs[Index] * NumFives;
ChiSqVal := ChiSqVal + (FMathUtils.Sqr(Expected - Bucket[Index]) / Expected);
end;
// Return values
FChiScore := ChiSqVal;
FDegsFreedom := 3;
FCurrentTestType := ttPoker;
end;
// The uniformity test
//--------------------
// The random numbers are partitioned into a number of equally sized
// buckets between 0.0 and 1.0. On the average, each bucket should have
// the same number of random numbers; ie they should be evenly spread
// over the range [0.0, 0.1). Apply Chi-Squared test to the buckets.
procedure TcaRandomTest.RunUniformityTest;
var
Bucket: array [0..Pred(cUniformityIntervals)] of Integer;
BucketNumber: Integer;
ChiSqVal: Double;
Expected: Double;
Index: Integer;
begin
// Fill buckets
FillChar(Bucket, SizeOf(Bucket), 0);
for Index := 0 to Pred(cUniformityCount) do
begin
BucketNumber := FMathUtils.Trunc(FRandom.AsDouble * cUniformityIntervals);
Inc(Bucket[BucketNumber]);
end;
// Calculate chi squared
Expected := cUniformityCount / cUniformityIntervals;
ChiSqVal := 0.0;
for Index := 0 to Pred(cUniformityIntervals) do
ChiSqVal := ChiSqVal + (FMathUtils.Sqr(Expected - Bucket[Index]) / Expected);
// Return values
FChiScore := ChiSqVal;
FDegsFreedom := Pred(cUniformityIntervals);
FCurrentTestType := ttUniformity;
end;
// Property methods