hedgewars/uRandom.pas
author unc0rr
Thu, 05 Oct 2006 20:13:51 +0000
changeset 190 206aabea2229
parent 183 57c2ef19f719
child 320 1ee7f087195a
permissions -rw-r--r--
Alpha channel value fix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     1
(*
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     2
 * Hedgewars, a worms-like game
136
89970b70b076 Implement bot levels
unc0rr
parents: 130
diff changeset
     3
 * Copyright (c) 2004-2006 Andrey Korotaev <unC0Rr@gmail.com>
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     4
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     8
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    12
 * GNU General Public License for more details.
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    13
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    14
 * You should have received a copy of the GNU General Public License
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    15
 * along with this program; if not, write to the Free Software
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    17
 *)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    18
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    19
unit uRandom;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    20
interface
124
75b892eff74d Fixed PRNG to properly use seed string
unc0rr
parents: 107
diff changeset
    21
{$INCLUDE options.inc}
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    22
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    23
procedure SetRandomSeed(Seed: shortstring);
107
b08ce0293a51 - Many type fixes
unc0rr
parents: 105
diff changeset
    24
function  GetRandom: Double; overload;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    25
function  GetRandom(m: LongWord): LongWord; overload;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    26
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    27
implementation
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    28
var cirbuf: array[0..63] of Longword;
124
75b892eff74d Fixed PRNG to properly use seed string
unc0rr
parents: 107
diff changeset
    29
    n: byte = 54;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    30
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    31
function GetNext: Longword;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    32
begin
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    33
n:= (n + 1) and $3F;
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    34
cirbuf[n]:=
105
e7cb9bb4a9de - Fixed integer->longint
unc0rr
parents: 102
diff changeset
    35
           (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
e7cb9bb4a9de - Fixed integer->longint
unc0rr
parents: 102
diff changeset
    36
            cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
e7cb9bb4a9de - Fixed integer->longint
unc0rr
parents: 102
diff changeset
    37
            and $7FFFFFFF;                       {mod 2^31}
136
89970b70b076 Implement bot levels
unc0rr
parents: 130
diff changeset
    38
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    39
Result:= cirbuf[n]
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    40
end;
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    41
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    42
procedure SetRandomSeed(Seed: shortstring);
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    43
var i: Longword;
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    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
75b892eff74d Fixed PRNG to properly use seed string
unc0rr
parents: 107
diff changeset
    47
if Length(Seed) > 54 then Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    48
124
75b892eff74d Fixed PRNG to properly use seed string
unc0rr
parents: 107
diff changeset
    49
for i:= 0 to pred(Length(Seed)) do
75b892eff74d Fixed PRNG to properly use seed string
unc0rr
parents: 107
diff changeset
    50
    cirbuf[i]:= byte(Seed[i + 1]) * (i + 1);
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    51
124
75b892eff74d Fixed PRNG to properly use seed string
unc0rr
parents: 107
diff changeset
    52
for i:= Length(Seed) to 54 do
130
19e3c16fb9f0 Fix engine PRNG (accidentally deleted a line)
unc0rr
parents: 124
diff changeset
    53
    cirbuf[i]:= i * 7 + 1;
19e3c16fb9f0 Fix engine PRNG (accidentally deleted a line)
unc0rr
parents: 124
diff changeset
    54
19e3c16fb9f0 Fix engine PRNG (accidentally deleted a line)
unc0rr
parents: 124
diff changeset
    55
for i:= 0 to 1023 do GetNext
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    56
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    57
107
b08ce0293a51 - Many type fixes
unc0rr
parents: 105
diff changeset
    58
function GetRandom: Double;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    59
begin
124
75b892eff74d Fixed PRNG to properly use seed string
unc0rr
parents: 107
diff changeset
    60
Result:= frac( GetNext * 0.00073 + GetNext * 0.00301)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    61
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    62
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    63
function GetRandom(m: LongWord): LongWord;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    64
begin
105
e7cb9bb4a9de - Fixed integer->longint
unc0rr
parents: 102
diff changeset
    65
GetNext;
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    66
Result:= GetNext mod m
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    67
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    68
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    69
end.