-
Notifications
You must be signed in to change notification settings - Fork 9
/
EZDSLRnd.PAS
430 lines (398 loc) · 10.9 KB
/
EZDSLRnd.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
{===EZDSLRND==========================================================
Part of the Delphi Structures Library--the random number generator
Copyright (c) 1993-2015, Julian M Bucknall
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=====================================================================}
unit EzdslRnd;
{$I EzdslDef.inc}
{---Place any compiler options you require here----------------------}
{--------------------------------------------------------------------}
{$I EzdslOpt.inc}
interface
uses
{$IFDEF ThreadsExist}
EzdslThd,
{$ENDIF}
SysUtils;
type
DWORD = longint;
type
TEZRandomGenerator = class
private
rgList : pointer;
{$IFDEF ThreadsExist}
rgResLock : TezResourceLock;
{$ENDIF}
protected
public
constructor Create;
{-Create the generator}
destructor Destroy; override;
{-Destroy the generator}
procedure AcquireAccess;
{-Lock the generator in a multithreaded process}
procedure ReleaseAccess;
{-Unlock the generator in a multithreaded process}
procedure SetSeed(const aSeed : longint);
{-Reseed the generator, if aSeed is zero the generator reseeds
from the system clock}
function Random : double;
{-Return a random number in the range: 0.0 <= R < 1.0}
function RandomByte : byte;
{-Return a random byte in the range: 0 <= R < 256}
function RandomWord : word;
{-Return a random word in the range: 0 <= R < 65536}
function RandomLong : longint;
{-Return a random longint in the range: 0 <= R < 2,147,483,648}
function RandomDWord : DWORD;
{-Return a random dword in the range: 0 <= R < 4,294,967,296}
function RandomIntLimit(aUpperLimit : integer) : integer;
{-Return a random integer in the range: 0 <= R < aUpperLimit}
{ NOTE: no check is made to see whether aUpperLimit > 0}
function RandomIntRange(aLowerLimit, aUpperLimit : integer) : integer;
{-Return a random integer in the range: aLowerLimit <= R < aUpperLimit}
{ NOTE: no check is made to see whether aUpperLimit > aLowerLimit}
function RandomFloatLimit(aUpperLimit : double) : double;
{-Return a random double in the range: 0.0 <= R < aUpperLimit}
{ NOTE: no check is made to see whether aUpperLimit > 0}
function RandomFloatRange(aLowerLimit, aUpperLimit : double) : double;
{-Return a random double in the range: aLowerLimit <= R < aUpperLimit}
{ NOTE: no check is made to see whether aUpperLimit > aLowerLimit}
end;
implementation
{References:
Random bit generator from Numerical Recipes in Pascal
Additive random number generator from Knuth: Seminumerical
Algorithms
Random sequence validation:
Output from TEZRandomGenerator has been validated with the DIEHARD
suite, please see http://stat.fsu.edu/~geo/diehard.html for details}
uses
{$IFDEF Windows}
WinTypes,
WinProcs; {for DOS3Call}
{$ENDIF}
{$IFDEF Win32}
Windows; {for GetTickCount}
{$ENDIF}
{$IFDEF Linux}
Types,
Libc;
{$ENDIF}
const
{Values are selected from Knuth 3.2.2}
TableMagic = 24;
TableEntries = 55;
const
Scale : integer = -31;
type
PrgTable = ^TrgTable;
TrgTable = packed record
tFrmOfs : integer;
tToOfs : integer;
tEntries: array [0..pred(TableEntries)] of longint;
end;
{===Helper routines==================================================}
function Random32Bit(aSeed : longint) : longint;
{$IFDEF BASM32}
{Input: EAX = current seed
Output: EAX = 32-bit random value & new seed}
register;
asm
push ebx
mov ebx, eax
mov ecx, 32 {use ecx as the count}
@@NextBit:
mov edx, ebx
mov eax, ebx
shr edx, 1 {xor with bit 1 of seed}
xor eax, edx
shr edx, 1 {xor with bit 2 of seed}
xor eax, edx
shr edx, 2 {xor with bit 4 of seed}
xor eax, edx
shr edx, 2 {xor with bit 6 of seed}
xor eax, edx
shr edx, 25 {xor with bit 31 of seed}
xor eax, edx
and eax, 1 {isolate the new random bit}
shl ebx, 1 {shift seed left by one}
or ebx, eax {add in the new bit to the seed as bit 0}
dec ecx {go get next random bit, until we've got them all}
jnz @@NextBit
mov eax, ebx {return random bits}
pop ebx
end;
{$ENDIF}
{$IFDEF Windows}
near; assembler;
asm
mov dx, aSeed.Word[2]
mov bx, aSeed.Word[0]
mov cx, 32 {use cx as the count}
@@NextBit:
mov si, bx
mov ax, si {get bit 0 of seed}
shr si, 1 {xor with bit 1 of seed}
xor ax, si
shr si, 1 {xor with bit 2 of seed}
xor ax, si
shr si, 1 {xor with bit 4 of seed}
shr si, 1
xor ax, si
shr si, 1 {xor with bit 6 of seed}
shr si, 1
xor ax, si
mov si, dx {xor with bit 31 of seed}
shl si, 1
rcl si, 1
xor ax, si
and ax, 1 {isolate the new random bit}
shl bx, 1 {shift seed left by one}
rcl dx, 1
or bx, ax {add in the new bit to the seed as bit 0}
loop @@NextBit {go get next random bit, until we've got them all}
mov ax, bx {return new seed}
end;
{$ENDIF}
{--------}
procedure InitTable(aTable : PrgTable; aSeed : longint);
var
i : integer;
begin
with aTable^ do begin
tToOfs := pred(TableEntries);
tFrmOfs := pred(TableMagic);
for i := 0 to pred(TableEntries) do begin
aSeed := Random32bit(aSeed);
tEntries[i] := aSeed;
end;
end;
end;
{--------}
function GetNextRandomDWORD(aTable : PrgTable) : DWORD;
type
DWArray = array [0..1] of word;
var
i : integer;
ResultAsWords : DWArray absolute Result;
begin
with aTable^ do begin
for i := 0 to 1 do begin
inc(tEntries[tToOfs], tEntries[tFrmOfs]);
ResultAsWords[i] := DWArray(tEntries[tToOfs])[1];
if (tToOfs = 0) then begin
tToOfs := pred(TableEntries);
dec(tFrmOfs);
end
else begin
dec(tToOfs);
if (tFrmOfs = 0) then
tFrmOfs := pred(TableEntries)
else
dec(tFrmOfs);
end;
end;
end;
end;
{--------}
(****
function GetNextRandomWord(aTable : PrgTable) : Word;
begin
with aTable^ do begin
inc(tEntries[tToOfs], tEntries[tFrmOfs]);
Result := word(tEntries[tToOfs]);
if (tToOfs = 0) then begin
tToOfs := pred(TableEntries);
dec(tFrmOfs);
end
else begin
dec(tToOfs);
if (tFrmOfs = 0) then
tFrmOfs := pred(TableEntries)
else
dec(tFrmOfs);
end;
end;
end;
****)
{====================================================================}
{===TEZRandomGenerator===============================================}
constructor TEZRandomGenerator.Create;
begin
inherited Create;
GetMem(rgList, sizeof(TrgTable));
SetSeed(0);
{$IFDEF ThreadsExist}
rgResLock := TezResourceLock.Create;
{$ENDIF}
end;
{--------}
destructor TEZRandomGenerator.Destroy;
begin
if (rgList <> nil) then
FreeMem(rgList, sizeof(TrgTable));
{$IFDEF ThreadsExist}
rgResLock.Free;
{$ENDIF}
inherited Destroy;
end;
{--------}
procedure TEZRandomGenerator.AcquireAccess;
begin
{$IFDEF ThreadsExist}
rgResLock.Lock;
{$ENDIF}
end;
{--------}
procedure TEZRandomGenerator.ReleaseAccess;
begin
{$IFDEF ThreadsExist}
rgResLock.Unlock;
{$ENDIF}
end;
{--------}
function TEZRandomGenerator.Random : double;
{$IFDEF BASM32}
register;
asm
call RandomDword
shr eax, 1
push eax
fild Scale
fild dword ptr [esp]
fscale
fstp st(1)
pop eax
end;
{$ENDIF}
{$IFDEF Windows}
assembler;
var
R : longint;
Scale : integer;
asm
les di, Self
push di
push es
call RandomDword
shr dx, 1
rcr ax, 1
mov R.Word[0], ax
mov R.Word[2], dx
mov Scale, -31
fild Scale
fild R
fscale
fstp st(1)
fwait
end;
{$ENDIF}
{--------}
function TEZRandomGenerator.RandomByte : byte;
begin
Result := byte(GetNextRandomDWORD(PrgTable(rgList)));
end;
{--------}
function TEZRandomGenerator.RandomDWord : DWORD;
begin
Result := GetNextRandomDWORD(PrgTable(rgList));
end;
{--------}
function TEZRandomGenerator.RandomFloatLimit(aUpperLimit : double) : double;
begin
Result := Random * aUpperLimit;
end;
{--------}
function TEZRandomGenerator.RandomFloatRange(aLowerLimit, aUpperLimit : double) : double;
begin
Result := (Random * (aUpperLimit - aLowerLimit)) + aLowerLimit;
end;
{--------}
function TEZRandomGenerator.RandomIntLimit(aUpperLimit : integer) : integer;
{$IFDEF BASM32}
register;
asm
push edx
call RandomDWord
pop edx
mul edx
mov eax, edx
end;
{$ENDIF}
{$IFDEF Windows}
assembler;
asm
les di, Self
push di
push es
call RandomDword
mov ax, dx
mul aUpperLimit
mov ax, dx
end;
{$ENDIF}
{--------}
function TEZRandomGenerator.RandomIntRange(aLowerLimit, aUpperLimit : integer) : integer;
begin
Result := RandomIntLimit(aUpperLimit - aLowerLimit) + aLowerLimit;
end;
{--------}
function TEZRandomGenerator.RandomLong : longint;
begin
Result := GetNextRandomDWORD(PrgTable(rgList)) shr 1;
end;
{--------}
function TEZRandomGenerator.RandomWord : word;
begin
Result := word(GetNextRandomDWORD(PrgTable(rgList)));
end;
{--------}
procedure TEZRandomGenerator.SetSeed(const aSeed : longint);
var
SeedValue : longint;
begin
if (aSeed <> 0) then
SeedValue := aSeed
else begin
{$IFDEF Windows}
asm
mov ah, $2C
call DOS3Call
mov SeedValue.Word[0], cx
mov SeedValue.Word[2], dx
end;
{$ENDIF}
{$IFDEF Win32}
SeedValue := GetTickCount;
{$ENDIF}
{$IFDEF Linux}
!!!!
{$ENDIF}
end;
InitTable(PrgTable(rgList), SeedValue);
end;
{====================================================================}
end.