author | unc0rr |
Tue, 23 Jan 2007 16:59:05 +0000 | |
changeset 358 | 236bbd12d4d9 |
parent 351 | 29bc9c36ad5f |
child 393 | db01cc79f278 |
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 |
|
351 | 21 |
uses uFloat; |
124 | 22 |
{$INCLUDE options.inc} |
4 | 23 |
|
102 | 24 |
procedure SetRandomSeed(Seed: shortstring); |
351 | 25 |
function GetRandom: hwFloat; overload; |
4 | 26 |
function GetRandom(m: LongWord): LongWord; overload; |
27 |
||
28 |
implementation |
|
102 | 29 |
var cirbuf: array[0..63] of Longword; |
124 | 30 |
n: byte = 54; |
4 | 31 |
|
102 | 32 |
function GetNext: Longword; |
4 | 33 |
begin |
102 | 34 |
n:= (n + 1) and $3F; |
35 |
cirbuf[n]:= |
|
105 | 36 |
(cirbuf[(n + 40) and $3F] + {n - 24 mod 64} |
37 |
cirbuf[(n + 9) and $3F]) {n - 55 mod 64} |
|
38 |
and $7FFFFFFF; {mod 2^31} |
|
136 | 39 |
|
351 | 40 |
GetNext:= cirbuf[n] |
102 | 41 |
end; |
42 |
||
43 |
procedure SetRandomSeed(Seed: shortstring); |
|
44 |
var i: Longword; |
|
45 |
begin |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
46 |
n:= 54; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
47 |
|
124 | 48 |
if Length(Seed) > 54 then Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf |
102 | 49 |
|
320 | 50 |
for i:= 1 to Length(Seed) do |
51 |
cirbuf[i - 1]:= byte(Seed[i]) * i; |
|
102 | 52 |
|
124 | 53 |
for i:= Length(Seed) to 54 do |
130 | 54 |
cirbuf[i]:= i * 7 + 1; |
55 |
||
56 |
for i:= 0 to 1023 do GetNext |
|
4 | 57 |
end; |
58 |
||
351 | 59 |
function GetRandom: hwFloat; |
60 |
var r: hwFloat; |
|
4 | 61 |
begin |
351 | 62 |
GetNext; |
63 |
r.isNegative:= false; |
|
64 |
r.QWordValue:= GetNext; |
|
65 |
GetRandom:= r |
|
4 | 66 |
end; |
67 |
||
102 | 68 |
function GetRandom(m: LongWord): LongWord; |
4 | 69 |
begin |
105 | 70 |
GetNext; |
351 | 71 |
GetRandom:= GetNext mod m |
4 | 72 |
end; |
73 |
||
74 |
end. |