-
Notifications
You must be signed in to change notification settings - Fork 208
/
CSharpRNG.ahk
100 lines (91 loc) · 2.35 KB
/
CSharpRNG.ahk
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
Class CSharpRNG
{
static MBIG := 2147483647
static MSEED := 161803398
static MZ := 0
inext := 0
inextp := 0
SeedArray := []
__New(Seed)
{
seedArray := this.SeedArray
subtraction := (Seed == -2147483648) ? 2147483647 : Abs(Seed)
mj := this.MSEED - subtraction
seedArray[55] := mj
mk := 1
Loop, 54
{
ii := Mod((21 * A_Index), 55)
seedArray[ii] := mk
mk := mj - mk
if (mk < 0)
mk += this.MBIG
mj := seedArray[ii]
}
Loop, 4
{
Loop, 55
{
seedArray[A_Index] -= seedArray[1 + Mod(A_Index + 30, 55)]
if (seedArray[A_Index] < 0)
seedArray[A_Index] += this.MBIG
}
}
this.inext := 0
this.inextp := 21
}
Sample()
{
return this.InternalSample() / this.MBIG
}
InternalSample()
{
seedArray := this.SeedArray
locINext := this.inext
locINextp := this.inextp
if (++locINext >= 56)
locINext := 1
if (++locINextp >= 56)
locINextp := 1
retVal := seedArray[locINext] - seedArray[locINextp]
if (retVal == this.MBIG)
retVal--
if (retVal < 0)
retVal += this.MBIG
seedArray[locINext] := retVal
this.inext := locINext
this.inextp := locINextp
return retVal
}
Next()
{
return this.InternalSample()
}
GetSampleForLargeRange()
{
result := this.InternalSample()
if (Mod(this.InternalSample(), 2) == 0)
result := -result
return (result + 2147483646) / 4294967293
}
NextRange(minValue, maxValue)
{
if (minValue > maxValue)
throw "'" . minValue . "' cannot be greater than " . maxValue . "."
range := maxValue - minValue
if (range <= 2147483647)
return Floor(this.Sample() * range) + minValue
else
return Floor(this.GetSampleForLargeRange() * range + minValue)
}
NextPositive(maxValue)
{
if (maxValue < 0)
throw "'" . maxValue . "' must be greater than zero."
return Floor(this.Sample() * maxValue)
}
NextDouble()
{
return this.Sample()
}
}