home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ada
/
bd3.arc
/
RANDOM.LST
< prev
next >
Wrap
File List
|
1989-03-12
|
4KB
|
102 lines
1 package RANDOM is
2 --------------------------------------------------------------------------
3 --| BEGIN PROLOGUE
4 --| DESCRIPTION : Package RANDOM contains the function NUMBER
5 --| : which returns a pseudo-random number
6 --| : of type FLOAT in the range 0.0 .. 1.0.
7 --| :
8 --| REQUIREMENTS SUPPORTED : Random Number Generator
9 --| :
10 --| LIMITATIONS : None
11 --| :
12 --| AUTHOR(S) : Richard Conn (RLC) from Bill Whitaker's work
13 --| CHANGE LOG : 09/30/88 RLC Design, code, test from
14 --| : Bill Whitaker's original work
15 --| : 10/11/88 RLC Modified based on ideas from
16 --| : Ron Bell and his RAN2 Package
17 --| :
18 --| REMARKS : None
19 --| :
20 --| PORTABILITY ISSUES : Uses 16-bit integers, so should be quite
21 --| : portable
22 --| END PROLOGUE
23 --------------------------------------------------------------------------
24
25 function NUMBER return FLOAT;
26 -- Return a floating point pseudo-random number
27
28 end RANDOM;
29 --
30 with CALENDAR;
31 package body RANDOM is
32
33 X : INTEGER;
34 Y : INTEGER;
35 Z : INTEGER;
36
37 --=============================================================
38 function CONVERT_TO_FLOAT(ITEM : in INTEGER) return FLOAT is
39 -- This function is necessary for some optimizing compilers
40 -- in order to prevent expressions like FLOAT(INTEGER(FLOAT))
41 -- from being optimized away
42 begin
43 return FLOAT(ITEM);
44 end CONVERT_TO_FLOAT;
45
46 --=============================================================
47 procedure SEED is
48 -- Generate seed values for X, Y, and Z using Package CALENDAR
49 DAY_MONTH : FLOAT;
50 SECONDS : FLOAT;
51 HUNDREDS : FLOAT;
52
53 begin
54 SECONDS := FLOAT(CALENDAR.SECONDS(CALENDAR.CLOCK));
55 HUNDREDS := (SECONDS/2.88) -
56 CONVERT_TO_FLOAT(INTEGER((SECONDS/2.88) - 0.5));
57 DAY_MONTH := FLOAT(CALENDAR.DAY(CALENDAR.CLOCK) *
58 CALENDAR.MONTH(CALENDAR.CLOCK));
59 X := INTEGER(SECONDS/2.88);
60 Y := INTEGER(HUNDREDS * 30000.0);
61 Z := INTEGER(DAY_MONTH/372.0 * SECONDS * 30000.0);
62 end SEED;
63
64 --
65 -- Package body RANDOM
66
67 --=============================================================
68 function NUMBER return FLOAT is
69 -- This rectangular random number routine is adapted from a report
70 -- "A Pseudo-Random Number Generator" by B. A. Wichmann and I. D. Hill
71 -- NPL Report DNACS XX (to be published)
72 -- In this version, it is suitable for machines supporting
73 -- INTEGER at only 16 bits and is portable in Ada
74
75 W : FLOAT;
76
77 begin
78 X := 171 * (X mod 177) - 2 * (X / 177);
79 -- Used to be: X := 171 * (X mod 177 - 177) - 2 * (X / 177);
80 if X < 0 then
81 X := X + 30269;
82 end if;
83 Y := 172 * (Y mod 176) - 35 * (Y / 176);
84 if Y < 0 then
85 Y := Y + 30307;
86 end if;
87 Z := 170 * (Z mod 178) - 63 * (Z / 178);
88 if Z < 0 then
89 Z := Z + 30323;
90 end if;
91
92 W := FLOAT(X) / 30269.0 + FLOAT(Y) / 30307.0 + FLOAT(Z) / 30323.0;
93 return W - CONVERT_TO_FLOAT(INTEGER(W - 0.5));
94 end NUMBER;
95
96 --=============================================================
97 begin
98
99 SEED; -- Initialize random number generator
100
101 end RANDOM;