[comp.lang.modula2] random numbers?

v092pxca@ubvmsb.cc.buffalo.edu (Paul D Fly) (04/23/91)

Hello everyone,
	A quick question I was wondering if anyone could answer for me.  How
do you generate random numbers in Modula-2?  I've searched through quite a few
books and can't find a thing about it.
	Thanks in advance.
				Paul Fly

AL281785@VMTECSLP.BITNET (RoDoGu) (04/23/91)

These procedures are provided by JPI V1.xx in its LIB module.

CONST
  HistoryMax = 54;


VAR
  HistoryPtr : CARDINAL;
  LowerPtr   : CARDINAL;
VAR
  History    : ARRAY 0..HistoryMax| OF CARDINAL;

PROCEDURE SetUpHistory(Seed: CARDINAL);
VAR
  x : LONGCARD;
  i : CARDINAL;
BEGIN
  HistoryPtr := HistoryMax;
  LowerPtr := 23;
  x := LONGCARD(Seed);
  i := 0;
  REPEAT
    x := (x*3141592621+17);
    Historyi| := CARDINAL(x DIV 10000H);
    INC(i);
  UNTIL i>HistoryMax;
END SetUpHistory;

PROCEDURE RANDOM(Range: CARDINAL) : CARDINAL;
VAR res:CARDINAL;
BEGIN
  IF HistoryPtr = 0 THEN
    IF LowerPtr = 0 THEN
      SetUpHistory(12345);
    ELSE
      HistoryPtr := HistoryMax;
      LowerPtr   := LowerPtr-1;
    END;
  ELSE
    HistoryPtr := HistoryPtr-1;
    IF LowerPtr = 0 THEN
      LowerPtr := HistoryMax;
    ELSE
      LowerPtr := LowerPtr-1;
    END;
  END;
  res := HistoryHistoryPtr|+HistoryLowerPtr|;
  HistoryHistoryPtr| := res;
  IF Range = 0 THEN
    RETURN res;
  ELSE
    RETURN res MOD Range;
  END;
END RANDOM;

PROCEDURE RANDOMIZE;
VAR R : Registers;
BEGIN
  WITH R DO
    AH := 2CH;
    Dos(R);
    SetUpHistory(DX+CX);
  END;
END RANDOMIZE;

PROCEDURE RAND(): REAL;
VAR
  x:RECORD low,high:CARDINAL END;
BEGIN
  x.low := RANDOM(0);
  x.high := RANDOM(0);
  RETURN REAL(LONGCARD(x))/(REAL(MAX(LONGCARD))+1.0);
END RAND;


     So you can use this generator (its name is Metodo de Congruencia, I don't
know how to it's say in English). I used it in my Simulation Class.


          X    = a X   MOD m
           k+1      k

    WHERE :

        a := 200T +- Q
        T = N+ (odd)
        Q = { 3,11,13,19,21,27,29,37,53,61,67,69,77,91...} (* I don't now why
                                                              these values *)
              d
        m = 10    d is the number of digits you need


   the period of this metod is  :
                       (d - 2)
                t * 10


   For example :

        d := 4; T := 1 q := 91;  Seed = 0417

        a := 200(1) + 91 = 291;
        x0 := 0417
        x1 := 291(0417) MOD 10000 = 1347
        -
        -
        -

The only problem of this metod is that the last digit its allways the same. So

you can use your imagination to aviod this problem


I hope this could help you

============================================================================
Instituto Tecnologico y de Estudios Superiores de Monterrey, Campus San Luis
MEXICO
Roberto Dominguez Gutierrez, AL-ISC

LONG LIVE JPI V1.xx, what about V3.0?

news@PSG.COM (Randy Bush) (04/25/91)

In comp.lang.modula2 you write:

> These procedures are provided by JPI V1.xx in its LIB module.

And my copy of the diskette has a Copyright notice on it, which means you
should not be posting it to this newsgroup.

randy--
Randy Bush  / news@psg.com  / ..!uunet!m2xenix!news