-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPK.Math.Random.RandomManager.pas
More file actions
123 lines (104 loc) · 2.51 KB
/
Copy pathPK.Math.Random.RandomManager.pas
File metadata and controls
123 lines (104 loc) · 2.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
(*
* Random Manager
*
* PLATFORMS
* Windows / macOS / Android / iOS
*
* LICENSE
* Copyright (c) 2024 piksware (http://piksware.com/)
* Released under the MIT license
* http://opensource.org/licenses/mit-license.php
*
* 2024/12/12 Version 1.0.0
* Programmed by HOSOKAWA Jun (twitter: @pik)
*)
unit PK.Math.Random.RandomManager;
interface
uses
System.Classes
, System.Generics.Collections
, System.SysUtils
, PK.Math.Random.Types
;
type
TRandomManager = class
private class var
FCurrent: TRandomManager;
FBuilders: TDictionary<String, IRandomAlgoBuilder>;
private
class procedure Initialize;
class destructor DestroyClass;
class function ExecuteProc: UInt32; static;
class procedure SetSeedProc(ASeed: UInt64); static;
public
class procedure RegisterAlgo(
const AName: String;
const ABuilder: IRandomAlgoBuilder);
class property Current: TRandomManager read FCurrent;
private var
FAlgo: IRandomAlgo;
private
constructor Create; reintroduce;
public
procedure SetAlgo(const AName: String);
property Algo: IRandomAlgo read FAlgo;
end;
implementation
uses
PK.Math.Random.CNG
, PK.Math.Random.XorShift
, PK.Math.Random.MersenneTwister
, PK.Math.Random.LCG
;
{ TRandomManager }
constructor TRandomManager.Create;
begin
inherited;
end;
class destructor TRandomManager.DestroyClass;
begin
FBuilders.Free;
FCurrent.Free;
end;
class function TRandomManager.ExecuteProc: UInt32;
begin
if FCurrent.FAlgo = nil then
Result := 0
else
Result := FCurrent.FAlgo.Execute;
end;
class procedure TRandomManager.Initialize;
begin
if TRandomManager.FCurrent <> nil then
Exit;
TRandomManager.FCurrent := TRandomManager.Create;
FBuilders := TDictionary<String, IRandomAlgoBuilder>.Create;
Random32Proc := ExecuteProc;
RandomizeProc := SetSeedProc;
end;
class procedure TRandomManager.RegisterAlgo(
const AName: String;
const ABuilder: IRandomAlgoBuilder);
begin
TRandomManager.Initialize;
FCurrent.FBuilders.AddOrSetValue(AName, ABuilder);
end;
procedure TRandomManager.SetAlgo(const AName: String);
begin
if (FAlgo <> nil) and SameText(FAlgo.GetName, AName) then
Exit;
var Builder: IRandomAlgoBuilder;
if FBuilders.TryGetValue(AName, Builder) then
begin
FAlgo := Builder.CreateAlgo;
FAlgo.Initialize;
end;
end;
class procedure TRandomManager.SetSeedProc(ASeed: UInt64);
begin
if FCurrent.FAlgo <> nil then
FCurrent.FAlgo.SetSeed(ASeed);
end;
initialization
TRandomManager.Initialize;
end.