1 (* RandomNumber.mod implement a set of random number procedures for pervasive types.
3 Copyright (C) 2012-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
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)
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.
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.
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/>. *)
27 IMPLEMENTATION MODULE RandomNumber ;
30 FROM libc IMPORT rand, srand ;
31 FROM Selective IMPORT Timeval, InitTime, KillTime, GetTime, GetTimeOfDay ;
35 Randomize - initialize the random number generator with a seed
36 based on the microseconds.
48 GetTime(t, sec, usec) ;
55 RandomInit - initialize the random number generator with value, seed.
58 PROCEDURE RandomInit (seed: CARDINAL) ;
65 RandomBytes - fills in an array with random values.
68 PROCEDURE RandomBytes (VAR a: ARRAY OF BYTE) ;
75 a[i] := VAL(BYTE, rand()) ;
82 RandomInt - return an INTEGER in the range [low .. high].
85 PROCEDURE RandomInt (low, high: INTEGER) : INTEGER ;
87 RETURN VAL(INTEGER, RandomLongInt(low, high))
92 RandomShortInt - return an SHORTINT in the range [low..high].
95 PROCEDURE RandomShortInt (low, high: SHORTINT) : SHORTINT ;
97 RETURN VAL(SHORTINT, RandomInt(low, high))
102 RandomLongInt - return an LONGINT in the range [low..high].
105 PROCEDURE RandomLongInt (low, high: LONGINT) : LONGINT ;
111 RandomBytes(number) ;
112 IF (low=0) AND (high=0)
117 random := number MOD (values+1) ;
124 RandomLongCard - return an LONGCARD in the range [low..high].
127 PROCEDURE RandomLongCard (low, high: LONGCARD) : LONGCARD ;
133 RandomBytes(number) ;
134 IF (low=0) AND (high=0)
139 random := number MOD (values+1) ;
146 RandomCard - return a CARDINAL in the range [low..high].
149 PROCEDURE RandomCard (low, high: CARDINAL) : CARDINAL ;
151 RETURN RandomLongCard(low, high)
156 RandomShortCard - return a SHORTCARD in the range [low..high].
159 PROCEDURE RandomShortCard (low, high: CARDINAL) : CARDINAL ;
161 RETURN RandomLongCard(low, high)
162 END RandomShortCard ;
166 RandomReal - return a REAL number in the range 0.0..1.0
169 PROCEDURE RandomReal () : REAL ;
171 RETURN RandomLongReal()
176 RandomLongReal - return a LONGREAL number in the range 0.0..1.0
179 PROCEDURE RandomLongReal () : LONGREAL ;
184 RETURN VAL(LONGREAL, l)/VAL(LONGREAL, MAX(LONGCARD))
189 RandomShortReal - return a SHORTREAL number in the range 0.0..1.0
192 PROCEDURE RandomShortReal () : SHORTREAL ;
194 RETURN RandomLongReal()
195 END RandomShortReal ;