home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DP Tool Club 8
/
CDASC08.ISO
/
NEWS
/
554
/
JUIN
/
RANDOM.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-10-07
|
2KB
|
60 lines
{─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
Msg : 333 of 389
From : Erik Johnson 1:104/28.0 15 Jun 93 18:43
To : Ben Glazer
Subj : RANDOM NUMBER GENERATOR
────────────────────────────────────────────────────────────────────────────────
> EJ> The random number generator in Pascal is not very
> EJ> good. I have one that
> EJ> takes 3 seeds and will not repeat for some trillion or so numbers.
> EJ> can supply you with the code if you are interested.
>
>I'd like to see this one, Erik! Please, post away...
Here is the one I started with (as copied out of some long forgotten
programming journal). Unfortunately it is uncommented, and we are
advised by the original columnist that modying the results can cripple
the 'randomness' unless one knows they are doing. This produces
something like 'billions' of random numbers w/o repeating. I have since
modified (object oriented, longints...) but this version is smaller and
simpler.}
unit random;
{$D-}
interface
type
seedtype=record
seed1,seed2,seed3:word
end;
function ran(range:word):integer;
procedure getseed(var s1,s2,s3:word);
procedure setseed(s1,s2,s3:word);
procedure incseed(num:longint);
var randomseed:seedtype;
implementation
function ran(range:word):integer;
begin
with randomseed do begin
seed1:=seed1*179 mod 32771;
seed2:=seed2*183 mod 32779;
seed3:=seed3*182 mod 32783
end;
ran:=0;
if range>1 then
ran:=(randomseed.seed1+randomseed.seed2+randomseed.seed3) mod range
end;
** The rest got garbled, but they aren't crucial. Procs for setting and
getting the 3 current seed values, and incrementing the current seed
values without getting a random number back.
I'm not sure how the numerical constants in the RAN function are
derived, but I don't recommend changing them, unless you know how
they're derived.