mirror of
https://github.com/vishapoberon/compiler.git
synced 2026-04-06 09:52:24 +00:00
added oocRandomNumbers, updated newt wrapper with GetKey function. -- noch
Former-commit-id: ab5d3f734e
This commit is contained in:
parent
7de984b46a
commit
6886a243a6
14 changed files with 109 additions and 7 deletions
75
src/lib/ooc/oocRandomNumbers.Mod
Normal file
75
src/lib/ooc/oocRandomNumbers.Mod
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
(* $Id: RandomNumbers.Mod,v 1.1 1997/02/07 07:45:32 oberon1 Exp $ *)
|
||||
MODULE oocRandomNumbers;
|
||||
(*
|
||||
For details on this algorithm take a look at
|
||||
Park S.K. and Miller K.W. (1988). Random number generators, good ones are
|
||||
hard to find. Communications of the ACM, 31, 1192-1201.
|
||||
*)
|
||||
|
||||
CONST
|
||||
modulo* = 2147483647; (* =2^31-1 *)
|
||||
|
||||
VAR
|
||||
z : LONGINT;
|
||||
|
||||
PROCEDURE GetSeed* (VAR seed : LONGINT);
|
||||
(* Returns the currently used seed value. *)
|
||||
BEGIN
|
||||
seed := z
|
||||
END GetSeed;
|
||||
|
||||
PROCEDURE PutSeed* (seed : LONGINT);
|
||||
(* Set 'seed' as the new seed value. Any values for 'seed' are allowed, but
|
||||
values beyond the intervall [1..2^31-2] will be mapped into this range. *)
|
||||
BEGIN
|
||||
seed := seed MOD modulo;
|
||||
IF (seed = 0) THEN
|
||||
z := 1
|
||||
ELSE
|
||||
z := seed
|
||||
END
|
||||
END PutSeed;
|
||||
|
||||
PROCEDURE NextRND;
|
||||
CONST
|
||||
a = 16807;
|
||||
q = 127773; (* m div a *)
|
||||
r = 2836; (* m mod a *)
|
||||
VAR
|
||||
lo, hi, test : LONGINT;
|
||||
BEGIN
|
||||
hi := z DIV q;
|
||||
lo := z MOD q;
|
||||
test := a * lo - r * hi;
|
||||
IF (test > 0) THEN
|
||||
z := test
|
||||
ELSE
|
||||
z := test + modulo
|
||||
END
|
||||
END NextRND;
|
||||
|
||||
PROCEDURE RND* (range : LONGINT) : LONGINT;
|
||||
(* Calculates a new number. 'range' has to be in the intervall
|
||||
[1..2^31-2]. Result is a number from 0,1,..,range-1. *)
|
||||
BEGIN
|
||||
NextRND;
|
||||
RETURN z MOD range
|
||||
END RND;
|
||||
|
||||
PROCEDURE Random*() : REAL;
|
||||
(* Calculates a number x with 0.0 <= x < 1.0. *)
|
||||
BEGIN
|
||||
NextRND;
|
||||
RETURN (z-1)*(1 / (modulo-1))
|
||||
END Random;
|
||||
|
||||
(*
|
||||
PROCEDURE Randomize*;
|
||||
BEGIN
|
||||
PutSeed (Unix.time (Unix.NULL))
|
||||
END Randomize;
|
||||
*)
|
||||
|
||||
BEGIN
|
||||
z := 1
|
||||
END oocRandomNumbers.
|
||||
Loading…
Add table
Add a link
Reference in a new issue