An old chestnut - Random numbers 
Author Message
 An old chestnut - Random numbers

Hi,

I'm using the Mocka M2 compiler on a Linux box.   In the FAQ, I've seen
random number generator routines but the ones I've found are all for the fst
m2 compiler for DOS.   Can anyone point me toward a suitable clone for the
Mocka system.   I just don't have the time to try and fiddle the routines to
work.

Cheers
Rob



Tue, 07 Oct 2003 06:42:19 GMT  
 An old chestnut - Random numbers

Quote:

>I'm using the Mocka M2 compiler on a Linux box.   In the FAQ, I've seen
>random number generator routines but the ones I've found are all for the fst
>m2 compiler for DOS.   Can anyone point me toward a suitable clone for the
>Mocka system.   I just don't have the time to try and fiddle the routines to
>work.

Out of the FAQ:

A4. A pseudo random generator using the subtractive method taken from
Knuth, Seminumerical Algorithms, 3.2.2 and 3.6, belongs to Ulm's
Modula-2 System that is distributed under the terms of the GPL (and
in case of the library under the terms of the LGPL). More
informations may be found at
http://www.mathematik.uni-ulm.de/modula/ and
http://www.mathematik.uni-ulm.de/modula/man/man3/RandomGenerator.html

If you do not want to download the whole system, I am willing to
send you the sources of the random generator by private email.

Andreas.

--
Andreas Borchert, Universitaet Ulm, SAI, Helmholtzstr. 18, 89069 Ulm,  Germany

WWW:    http://www.mathematik.uni-ulm.de/sai/borchert/
PGP:    http://www.mathematik.uni-ulm.de/sai/borchert/pgp.html



Tue, 07 Oct 2003 15:42:48 GMT  
 An old chestnut - Random numbers

Quote:

> I'm using the Mocka M2 compiler on a Linux box.   In the FAQ, I've seen
> random number generator routines but the ones I've found are all for the fst
> m2 compiler for DOS.   Can anyone point me toward a suitable clone for the
> Mocka system.   I just don't have the time to try and fiddle the routines to
> work.

As it happened, I did that job soime time ago for the Mocka-System.
Should would also in other Modula-2 environments... Just use the
shar archive below. Sorry, no further documentation (that was in
the student handouts) on usage, but it should be obvious. Otherwise,
read the cited CACM article on pseudo-random numbers!

Karl Kleine

###########################################################################

#!/bin/sh
# This is a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#

# Source directory /fhj/gw/kleine/edu
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#   1023 -rw-r--r-- PMmsRNG.md
#   1187 -rw-r--r-- PMmsRNG.mi
#
# ============= PMmsRNG.md ==============
if test -f 'PMmsRNG.md' -a X"$1" != X"-c"; then
        echo 'x - skipping PMmsRNG.md (File already exists)'
else
echo 'x - extracting PMmsRNG.md (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'PMmsRNG.md' &&
(*      PMmsRNG:   Park and Miller Minimal Standard Random Number Generator
**
**      see CACM vol 31#10, October 1988, pp.1192-1201
**
**      transliterated to Modula-2 by Karl Kleine, FH Jena, Nov.1995
**
**
**      This pseudo-random number generator produces a sequence of
**      CARDINAL numbers in the range 0..2^31-1 (2.147.483.647), resp.
**      a REAL number in the range 0 to 1. It will work on computers
**      with a word length of 32 bits for INTEGERs and CARDINALs.
**
**      Note that this RNG produces a reproducible pseudo random
**      sequence suitable for simulations and simular applications.
**      This characteristic makes it unsuitable for uses that need
**      truly unpredictable random number sequences like cryptography.
**      You can however generate different sequences for applications
**      like games by setting the seed value for a sequence to some
**      otherwise derived value.
*)
X
DEFINITION MODULE PMmsRNG;
X
PROCEDURE NextRandomCard (): CARDINAL;
X
PROCEDURE NextRandomReal (): REAL;
X
PROCEDURE SetSeed (SeedValue: CARDINAL);
X
END PMmsRNG.
SHAR_EOF
chmod 0644 PMmsRNG.md ||
echo 'restore of PMmsRNG.md failed'
Wc_c="`wc -c < 'PMmsRNG.md'`"
test 1023 -eq "$Wc_c" ||
        echo 'PMmsRNG.md: original size 1023, current size' "$Wc_c"
fi
# ============= PMmsRNG.mi ==============
if test -f 'PMmsRNG.mi' -a X"$1" != X"-c"; then
        echo 'x - skipping PMmsRNG.mi (File already exists)'
else
echo 'x - extracting PMmsRNG.mi (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'PMmsRNG.mi' &&
(*      PMmsRNG:   Park and Miller Minimal Standard Random Number Generator
**
**      See CACM vol 31#10, October 1988, pp.1192-1201.
**      The code below is based on on their 'Integer Version 2' on p.1195.
**      Identifiers are kept the same as far as possible.
**
**      transliterated to Modula-2 by Karl Kleine, FH Jena, Nov.1995
**      compiler used: MOCKA from GMD Karlsruhe.
*)
X
IMPLEMENTATION MODULE PMmsRNG;
X
IMPORT MathLib;                 (* just for INTEGER->REAL conversion *)
X
CONST
X    a = 16807;
X    m = 2147483647;            (* MaxInt for 32-bit machine *)
X    q = 127773;                        (* m DIV a *)
X    r = 2836;                  (* m MOD a *)
X
VAR
X    seed : INTEGER;
X    RealM: REAL;
X
X
PROCEDURE NextRandomCard (): CARDINAL;
X    VAR
X       lo, hi, test: INTEGER;
X    BEGIN
X       hi := seed DIV q;
X       lo := seed MOD q;
X       test := a * lo - r * hi;
X       IF test > 0 THEN
X           seed := test
X       ELSE
X           seed := test + m
X       END;
X       RETURN (VAL(CARDINAL,seed))
X    END NextRandomCard;
X
X
PROCEDURE NextRandomReal (): REAL;
X    BEGIN
X       RETURN (MathLib.real(NextRandomCard())/RealM)
X    END NextRandomReal;
X
X
PROCEDURE SetSeed (SeedValue: CARDINAL);
X    BEGIN
X       seed := SeedValue MOD m
X    END SetSeed;
X
X
BEGIN
X    seed := m DIV 2;
X    RealM := MathLib.real(m)
X
END PMmsRNG.
SHAR_EOF
chmod 0644 PMmsRNG.mi ||
echo 'restore of PMmsRNG.mi failed'
Wc_c="`wc -c < 'PMmsRNG.mi'`"
test 1187 -eq "$Wc_c" ||
        echo 'PMmsRNG.mi: original size 1187, current size' "$Wc_c"
fi
exit 0



Tue, 07 Oct 2003 17:55:55 GMT  
 An old chestnut - Random numbers
This is a 32 bit random number generator that is system independent:

Random.def:
(*
 * 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.
 *)

PROCEDURE Real(): REAL;
(*
 * Returns a random REAL number in the range [0..1)
 *)

PROCEDURE Seed(): INTEGER;
(*
 * Returns the current seed of the random number generator
 *)

PROCEDURE SetSeed(newSeed: INTEGER);
(*
 * Sets the seed of the random number generator
 *)

PROCEDURE GetAM (VAR currA, currM: INTEGER);
(*
 * Gets the current values for the multiplier (a) and modulus (m) used
in determining the random sequence
 *)

PROCEDURE SetAM (newA, newM: INTEGER);
(*
 * Sets new values for the multiplier (a) and modulus (m) used in
determining the random sequence.  Note that many values are NOT good
values.  The defaults have been proven to generate a full period.
 *)

END Random.

Random.mod:
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.
 *)
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     Work: seurer AT us.ibm.com      Home: Bill AT seurer.net



Tue, 07 Oct 2003 22:13:32 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Random Number Generator to produce SAME random number from 12:00am-11:59pm

2. old 'chestnut' - bar codes on reports

3. A old chestnut? 5.3 v 5.2e

4. File handles - that hoary old chestnut

5. Specifying local and external block parameters (that old chestnut)

6. Old chestnut: invariants, pre/post conditions

7. Old chestnut: invariants, pre/post conditions and so on

8. Random number not random?

9. Pentium III processor number and random number generator

10. J random-number generator: what is used?

11. Quasi Random numbers

12. How do I get random numbers with ALC?

 

 
Powered by phpBB® Forum Software