Debian Codegraph @

gcc/m2/gm2-libs-log/Random.mod

raw
1 (* Random.mod provides a Logitech-3.0 compatible library.
2
3 Copyright (C) 2005-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE Random ;
28
29 FROM libc IMPORT rand, srand ;
30 FROM Selective IMPORT Timeval, InitTime, KillTime, GetTime, GetTimeOfDay ;
31
32
33 (*
34 Randomize - initialize the random number generator with a seed
35 based on the microseconds.
36 *)
37
38 PROCEDURE Randomize ;
39 VAR
40 t : Timeval ;
41 sec, usec: CARDINAL ;
42 BEGIN
43 t := InitTime (0, 0) ;
44 IF GetTimeOfDay (t) = 0
45 THEN
46 END ;
47 GetTime (t, sec, usec) ;
48 RandomInit (usec) ;
49 t := KillTime (t)
50 END Randomize ;
51
52
53 (*
54 RandomInit - initialize the random number generator with value, seed.
55 *)
56
57 PROCEDURE RandomInit (seed: CARDINAL) ;
58 BEGIN
59 srand (seed)
60 END RandomInit ;
61
62
63 (*
64 RandomBytes - fills in an array with random values.
65 *)
66
67 PROCEDURE RandomBytes (VAR a: ARRAY OF BYTE) ;
68 VAR
69 i, h: CARDINAL ;
70 BEGIN
71 h := HIGH (a) ;
72 i := 0 ;
73 WHILE i <= h DO
74 a[i] := VAL (BYTE, rand ()) ;
75 INC (i)
76 END
77 END RandomBytes ;
78
79
80 (*
81 RandomInt - return an INTEGER in the range 0..bound-1
82 *)
83
84 PROCEDURE RandomInt (bound: INTEGER) : INTEGER ;
85 BEGIN
86 IF bound=0
87 THEN
88 RETURN rand ()
89 ELSE
90 RETURN rand () MOD bound
91 END
92 END RandomInt ;
93
94
95 (*
96 RandomCard - return a CARDINAL in the range 0..bound-1
97 *)
98
99 PROCEDURE RandomCard (bound: CARDINAL) : CARDINAL ;
100 VAR
101 c: CARDINAL ;
102 BEGIN
103 RandomBytes (c) ;
104 RETURN c MOD bound
105 END RandomCard ;
106
107
108 (*
109 RandomReal - return a REAL number in the range 0.0..1.0
110 *)
111
112 PROCEDURE RandomReal () : REAL ;
113 BEGIN
114 RETURN RandomLongReal ()
115 END RandomReal ;
116
117
118 (*
119 RandomLongReal - return a LONGREAL number in the range 0.0..1.0
120 *)
121
122 PROCEDURE RandomLongReal () : LONGREAL ;
123 VAR
124 l: LONGCARD ;
125 BEGIN
126 RandomBytes (l) ;
127 RETURN VAL (LONGREAL, l) / VAL (LONGREAL, MAX (LONGCARD))
128 END RandomLongReal ;
129
130
131 BEGIN
132 Randomize
133 END Random.