home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols000 / vol021 / random.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1984-04-29  |  1.8 KB  |  86 lines

  1. (*$I+    [include Pascal prgm stmts]    *)
  2. Program RandomDummy;
  3. var    SEED1, SEED2 : INTEGER;
  4. (*
  5. ==============================================
  6.  PROGRAM TITLE:    Random Number Generator
  7.  
  8.  WRITTEN BY:    Raymond E. Penley
  9.  DATE WRITTEN:    27 June 1980
  10.  
  11.  WRITTEN FOR:    Pascal/Z Users Group
  12.  
  13.  SUMMARY:
  14.     Implements a Fibonacci series Random number generator.
  15.     RANDOM will return numbers from 0 to x
  16.  
  17.         Call as              Returns
  18.         -------            ------------
  19.         real := RANDOM(10);        0.0 to 10.0
  20.         real := RANDOM(112);    0.0 to 112.0
  21.          I := TRUNC(RANDOM(10));      0 to 10
  22.  
  23. **
  24.   Add these lines to your PASCAL source program:
  25.  
  26.     Procedure SEEDRAND; EXTERNAL;
  27.     Functin RANDOM(X: Integer): REAL; external;
  28.  
  29.   Also within the body of the main program
  30.   but BEFORE calling RANDOM(X);
  31.  
  32.       SEEDRAND;
  33.  
  34. ==============================================
  35. *)
  36.  
  37. PROCEDURE SEEDRAND;
  38. (* INITIAL VALUES FOR SEED1 AND SEED2 ARE HERE  *)
  39. (*
  40.     NAME RANDOM
  41.     ENTRY SEEDRAND,RANDOM
  42.  
  43. SEEDRAND:
  44. *)
  45. Begin
  46.    SEED1 := 10946;
  47.    SEED2 := 17711
  48. END;
  49.  
  50.  
  51.  
  52. Function Random(x: integer): real;
  53. (*
  54. GLOBAL
  55.   SEED1, SEED2 : INTEGER        *)
  56. CONST
  57.   factor = Maxint;
  58.   HALFINT = 16383; (* 1/2 OF MAXINT *)
  59. VAR
  60.   x1 : real;
  61.   temp1, temp2, HALF_ADDER : INTEGER;
  62. (*
  63. RANDOM:
  64. *)
  65. Begin
  66. (* Take 1/2 of the seeds for the comparison test *)
  67.   temp1 := SEED1 DIV 2;
  68.   temp2 := SEED2 DIV 2;
  69.   IF (temp1+temp2) >= HALFINT then{the number is too big -}
  70.     { scale it down }
  71.     HALF_ADDER := temp1 + temp2 - HALFINT
  72.   ELSE
  73.     HALF_ADDER := temp1 + temp2;
  74.   SEED1 := SEED2;
  75.   (* Restore from previous DIVision *)
  76.   SEED2 := HALF_ADDER * 2;
  77.   (*---Convert X to real and divide by factor---*)
  78.   x1 := ((X*1.0)/factor);
  79.   (*---Return random number scaled by factor---*)
  80.   RANDOM := ( SEED2 * x1 );
  81. End{ of RANDOM(X) };
  82. (*$I-    [turn off now]    *)
  83.  
  84. (*---dummy program---*)
  85. begin end.
  86.