home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
sa104os2.zip
/
SATHR104.ZIP
/
SATHER
/
LIBRARY
/
RND.SA
< prev
next >
Wrap
Text File
|
1995-02-05
|
5KB
|
130 lines
-- Copyright (C) International Computer Science Institute, 1994. COPYRIGHT --
-- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
-- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in --
-- the file "Doc/License" of the Sather distribution. The license is also --
-- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA. --
--------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
-- changes 1994/11/03 by Erik Schnetter
-- rnd.sa: Random numbers.
-------------------------------------------------------------------
class RND{GEN < $RANDOM_GEN} is
-- Random numbers.
shared s_gen: GEN;
attr gen: GEN;
create: SAME is res::=new; res.gen := #GEN; return(res) end;
init is s_gen := #GEN; end;
init(seed: INT) is
if void(self) then
if void(s_gen) then init end; s_gen.init(seed)
else gen.init(seed) end
end; -- init
int(l,u:INT):INT pre l <= u is
return(l+(((u-l+1).fltd)*uniform).floor.int) end;
uniform: FLTD is
-- A uniformly distributed double in `[0.,1.)'.
-- Uses default generator if void(self) (as in RND::uniform) else gen.
if void(self) then
if void(s_gen) then init end;
return(s_gen.get)
-- else return(gen.get) end; -- NLP
end; return(gen.get); -- NLP
end; -- uniform
end; -- class RND
-------------------------------------------------------------------
class RND is
include RND{MS_RANDOM_GEN};
end;
-------------------------------------------------------------------
type $RANDOM_GEN is
init(seed: INT);
-- Initialize the generator using nseed. Any `INT' value should be legal.
get:FLTD;
-- The next random value. Should be in `[0.,1.)'.
end;
-------------------------------------------------------------------
class MS_RANDOM_GEN < $RANDOM_GEN is
-- The "minimal standard" generator described in "Random Number
-- Generators: Good Ones are Hard to Find" by Stephen Park and
-- Keith Miller, Communications of the ACM, October 1988, Volume
-- 31, Number 10, p. 1192. Linear congruential, produces a value
-- in `[0.,1.)' including `0.' but not `1.' Any seed value in
-- the range `[1,2147483646]' is equally good.
-- Constants used in generator:
-- BEWARE!! Problems with order of initialization and
-- and double literals
-- const ms_a:FLTD:=16807.0d; -- `7^5'
-- const ms_m:FLTD:=2147483647.0d; -- `(2^31)-1', prime
-- const ms_md:FLTD:=(ms_m-1.0d); -- to avoid continual recomputation
const ms_a:FLTD:=16807.0d; -- `7^5'
shared ms_m:FLTD; -- `(2^31)-1', prime
shared ms_md:FLTD; -- to avoid continual recomputation
attr seed:INT; -- Current state of generator.
create:SAME is
-- A minimal standard generator with `seed=1'.
res ::= new; res.init(1); return(res) end;
init(nseed:INT) is
-- Initialize the generator.
seed:=1+(nseed-1).mod(2147483645);
ms_m := (2.pow(31)).fltd-1.0d;
ms_md := ms_m - 1.0d;
end; -- keep in legal range
get:FLTD is
-- Pseudo-random value in `[0.,1.)' generated by minimal std generator.
tmp:FLTD:=ms_a*(seed.fltd);
seed:=(tmp-(ms_m*((tmp/ms_m).floor))).int;
return(((seed-1).fltd)/ms_md); end; -- get
-- Original version
-- tmp:FLTD:=ms_a*(seed);
-- seed:=(tmp-(ms_m*((tmp/ms_m).int))).int;
-- return(((seed-1))/ms_md); end; -- get
end; -- class MS_RANDOM_GEN
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TEST_RND is
include TEST;
main is
-- Should test out the beginning of the sequence at least...
-- Don't know if these test results are correct. These are just
-- the current values that are being printed out.
-- If the test fails, it just indicates that something has
-- changed in double/int arithmetic, not necc. an error
class_name("RND");
r ::= RND::uniform;
test("random 1 See comment about errors",r.str,"0.999992");
r := RND::uniform;
test("random 2 See comment about errors",r.str,"0.868462");
res ::= "";
loop
15.times!;
res := res+(RND::uniform).str+" ";
end;
unchecked_test("Random numbers",res,"0.244397 0.575954 0.0630371 0.464864 0.97356 0.627308 0.166989 0.585985 0.652986 0.734896 0.391713 0.523141 0.433819 0.192019 0.260525 ");
finish;
end;
end; -- class TEST_RND
-------------------------------------------------------------------