author | unc0rr |
Wed, 25 Oct 2006 18:03:41 +0000 | |
changeset 203 | 0ee86f9d9ba6 |
parent 183 | 57c2ef19f719 |
child 320 | 1ee7f087195a |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
136 | 3 |
* Copyright (c) 2004-2006 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
183 | 5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
4 | 8 |
* |
183 | 9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
4 | 13 |
* |
183 | 14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
4 | 17 |
*) |
18 |
||
19 |
unit uRandom; |
|
20 |
interface |
|
124 | 21 |
{$INCLUDE options.inc} |
4 | 22 |
|
102 | 23 |
procedure SetRandomSeed(Seed: shortstring); |
107 | 24 |
function GetRandom: Double; overload; |
4 | 25 |
function GetRandom(m: LongWord): LongWord; overload; |
26 |
||
27 |
implementation |
|
102 | 28 |
var cirbuf: array[0..63] of Longword; |
124 | 29 |
n: byte = 54; |
4 | 30 |
|
102 | 31 |
function GetNext: Longword; |
4 | 32 |
begin |
102 | 33 |
n:= (n + 1) and $3F; |
34 |
cirbuf[n]:= |
|
105 | 35 |
(cirbuf[(n + 40) and $3F] + {n - 24 mod 64} |
36 |
cirbuf[(n + 9) and $3F]) {n - 55 mod 64} |
|
37 |
and $7FFFFFFF; {mod 2^31} |
|
136 | 38 |
|
102 | 39 |
Result:= cirbuf[n] |
40 |
end; |
|
41 |
||
42 |
procedure SetRandomSeed(Seed: shortstring); |
|
43 |
var i: Longword; |
|
44 |
begin |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
45 |
n:= 54; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
46 |
|
124 | 47 |
if Length(Seed) > 54 then Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf |
102 | 48 |
|
124 | 49 |
for i:= 0 to pred(Length(Seed)) do |
50 |
cirbuf[i]:= byte(Seed[i + 1]) * (i + 1); |
|
102 | 51 |
|
124 | 52 |
for i:= Length(Seed) to 54 do |
130 | 53 |
cirbuf[i]:= i * 7 + 1; |
54 |
||
55 |
for i:= 0 to 1023 do GetNext |
|
4 | 56 |
end; |
57 |
||
107 | 58 |
function GetRandom: Double; |
4 | 59 |
begin |
124 | 60 |
Result:= frac( GetNext * 0.00073 + GetNext * 0.00301) |
4 | 61 |
end; |
62 |
||
102 | 63 |
function GetRandom(m: LongWord): LongWord; |
4 | 64 |
begin |
105 | 65 |
GetNext; |
102 | 66 |
Result:= GetNext mod m |
4 | 67 |
end; |
68 |
||
69 |
end. |