Thread View: comp.lang.modula2
3 messages
3 total messages
Started by Risto Lehtonen
Mon, 01 Apr 1996 11:09
Random in Modula-2 ???
Author: Risto Lehtonen
Date: Mon, 01 Apr 1996 11:09
Date: Mon, 01 Apr 1996 11:09
14 lines
881 bytes
881 bytes
How can I use randomnumbers in Modula-2 ? I gess I have to make my own definition/implementation module ? Has anyone done it ? Could you mail it for me, please ? ------------------------------------------------------------------ Risto Lehtonen Ritzinkuja 1L69 Tuulimyllynkuja 15 * rlehtone@aton.abo.fi 20380 Turku 08350 Routio * Risto.Lehtonen@abo.fi Puh: 921-2388378 Puh: 912-321536 ------------------------------------------------------------------
Re: Random in Modula-2 ???
Author: Bill Seurer
Date: Mon, 01 Apr 1996 19:49
Date: Mon, 01 Apr 1996 19:49
82 lines
2384 bytes
2384 bytes
IMPLEMENTATION MODULE Random;
(*
* Generates random numbers using the Lehmer algorithm, f(z)=az MOD m. The default a is 16,807 and the default m is 2,147,483,647 (2**31-1).
* See "Random number Generators: Good Ones Are Hard To Find", Park, Stephen K., and Miller, Keith W., Communications of the ACM, October, 1988.
*
* In the descriptions of the procedures the bounds of the ranges are shown using:
* [ and ] to denote an end of a range that is inclusive (i.e., IS included in the range).
* ( and ) to denote an end of a range that is exclusive (i.e., ISN'T included in the range).
*)
VAR
seed: INTEGER;
a, m, q, r: INTEGER;
PROCEDURE Real(): REAL;
(*
* Returns a random REAL number in the range [0..1)
*)
VAR
lo, hi, test: INTEGER;
BEGIN
hi := seed DIV q;
lo := seed MOD q;
test := a * lo - r * hi;
IF test > 0 THEN
seed := test;
ELSE
seed := test + m;
END (*Else*);
RETURN FLOAT(seed) / FLOAT(m);
END Real;
PROCEDURE Seed(): INTEGER;
(*
* Returns the current seed of the random number generator
*)
BEGIN
RETURN seed;
END Seed;
PROCEDURE SetSeed(newSeed: INTEGER);
(*
* Sets the seed of the random number generator
*)
BEGIN
seed := newSeed;
END SetSeed;
PROCEDURE GetAM (VAR currA, currM: INTEGER);
(*
* Gets the current values for the multiplier (a) and modulus (m) used in determining the random sequence
*)
BEGIN
currA := a;
currM := m;
END GetAM;
PROCEDURE SetAM (newA, newM: INTEGER);
(*
* Sets new values for the multiplier (a) and modulus (m) used in determining the random sequence.
*)
BEGIN
a := newA;
m := newM;
q := m DIV a;
r := m MOD a;
END SetAM;
BEGIN
SetAM (16807, 2147483647);
SetSeed (1);
END Random.
--
- Bill Seurer ID Tools and Compiler Development IBM Rochester, MN
Business: BillSeurer@vnet.ibm.com Home: BillSeurer@aol.com
Re: Random in Modula-2 ???
Author: "Mark Morgan Lloyd "
Date: Wed, 03 Apr 1996 12:31
Date: Wed, 03 Apr 1996 12:31
7 lines
522 bytes
522 bytes
Which compiler? Mark Morgan Lloyd mark_tbu@cix.compulink.co.uk [Opinions above are the author's, not those of his employers or colleagues]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads