Debian Codegraph @

gcc/testsuite/gm2/examples/map/pass/Chance.mod

raw
1 (* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
2 Free Software Foundation, Inc. *)
3 (* This file is part of GNU Modula-2.
4
5 GNU Modula-2 is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
8 version.
9
10 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with gm2; see the file COPYING. If not, write to the Free Software
17 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *)
18 IMPLEMENTATION MODULE Chance ;
19
20
21 FROM Args IMPORT GetArg ;
22 FROM NumberIO IMPORT StrToCard ;
23
24 FROM StrIO IMPORT WriteString, WriteLn ;
25
26 (* FROM Random IMPORT RandomCard ; *)
27
28
29 CONST
30 MaxCard = 65535 ;
31 MaxRandom = 8000 ;
32 MaxIndex = 500 ;
33
34 TYPE
35 Index = RECORD
36 Start, (* Start of the Random list *)
37 End : CARDINAL ; (* End of the Random list *)
38 END ;
39
40 VAR
41 RandomIndex: ARRAY [0..MaxIndex] OF Index ;
42 Random : ARRAY [1..MaxRandom] OF CARDINAL ;
43 NoOfRandom : CARDINAL ; (* Number of random numbers in array Coords *)
44 NoOfIndices: CARDINAL ; (* Number of indices in RandomIndex *)
45
46
47 (*
48 InitRandom - Initializes a potential list of random numbers.
49 An index to this potential random number list is returned.
50 *)
51
52 PROCEDURE InitRandom () : CARDINAL ;
53 BEGIN
54 IF NoOfIndices=MaxIndex
55 THEN
56 WriteString('Too many random list indices in Module Chance') ;
57 WriteLn ;
58 HALT
59 ELSE
60 INC(NoOfIndices) ;
61 WITH RandomIndex[NoOfIndices] DO
62 Start := NoOfRandom+1 ;
63 End := 0
64 END ;
65 Add(NoOfIndices, 0) ; (* Dummy random no. that we keep *)
66 RETURN(NoOfIndices) (* for the life of this list. *)
67 END
68 END InitRandom ;
69
70
71 (*
72 KillRandom - Kills a complete list of random numbers.
73 *)
74
75 PROCEDURE KillRandom (RandomListIndex: CARDINAL) ;
76 BEGIN
77 IF NoOfIndices>0
78 THEN
79 (* Destroy index to Random list *)
80 WITH RandomIndex[RandomListIndex] DO
81 Start := 0 ;
82 End := 0
83 END ;
84 (*
85 If killed last Random index list see if we can garbage collect
86 previously killed middle indices.
87 *)
88 IF NoOfIndices=RandomListIndex
89 THEN
90 REPEAT
91 DEC(NoOfIndices)
92 UNTIL (NoOfIndices=0) OR (RandomIndex[NoOfIndices].Start#0)
93 END ;
94 NoOfRandom := RandomIndex[NoOfIndices].End
95 ELSE
96 WriteString('All Random lists have been killed - Module Chance') ;
97 WriteLn ;
98 HALT
99 END
100 END KillRandom ;
101
102
103 (*
104 AddRandom - places a list of numbers 1..n into the specified list.
105 *)
106
107 PROCEDURE AddRandom (RandomListIndex: CARDINAL; n: CARDINAL) ;
108 BEGIN
109 WHILE n>0 DO
110 Add(RandomListIndex, n) ;
111 DEC(n)
112 END
113 END AddRandom ;
114
115
116 PROCEDURE Add (RandomListIndex: CARDINAL; i: CARDINAL) ;
117 BEGIN
118 IF NoOfRandom=MaxRandom
119 THEN
120 WriteString('Too many random numbers in a list in Module Chance') ;
121 WriteLn ;
122 HALT
123 ELSE
124 INC(NoOfRandom) ;
125 Random[NoOfRandom] := i ;
126 WITH RandomIndex[RandomListIndex] DO
127 End := NoOfRandom
128 END
129 END
130 END Add ;
131
132
133 (*
134 GetAndDeleteRandom - Returns a random number from the
135 list and then it is deleted.
136 *)
137
138 PROCEDURE GetAndDeleteRandom (RandomListIndex: CARDINAL) : CARDINAL ;
139 VAR
140 i, j: CARDINAL ;
141 BEGIN
142 WITH RandomIndex[RandomListIndex] DO
143 i := Start+GetRand(End-Start+1) ; (* +1 for GetRand *)
144 j := i ;
145 REPEAT
146 IF Random[j]=0
147 THEN
148 INC(j) ;
149 IF j>End
150 THEN
151 j := Start
152 END
153 END
154 UNTIL (j=i) OR (Random[j]#0) ;
155 i := Random[j] ;
156 Random[j] := 0 (* Now delete this box *)
157 END ;
158 RETURN( i )
159 END GetAndDeleteRandom ;
160
161
162 (*
163 GetRand - returns a number between 0..n-1.
164 This routine is independant of the above routines.
165 *)
166
167 VAR
168 RandomSeed: CARDINAL ;
169 Num : ARRAY [0..9] OF CHAR ;
170
171 PROCEDURE GetRand (n: CARDINAL) : CARDINAL ;
172 BEGIN
173 (* $R- *)
174 RandomSeed := (RandomSeed*257 + 0ABCDH) MOD MaxCard ;
175 (* $R= *)
176 RETURN( RandomSeed MOD n )
177 (*
178 IF n<2
179 THEN
180 RETURN( 0 ) (* return 0 if n=0 or n=1 *)
181 ELSE
182 RETURN( RandomCard(n) )
183 END
184 *)
185 END GetRand ;
186
187
188 PROCEDURE Init ;
189 BEGIN
190 NoOfRandom := 0 ;
191 NoOfIndices := 0 ;
192 WITH RandomIndex[NoOfIndices] DO
193 End := 0
194 END
195 END Init ;
196
197
198 BEGIN
199 Init
200 ; IF GetArg(Num, 1)
201 THEN
202 StrToCard(Num, RandomSeed)
203 ELSE
204 RandomSeed := 3
205 END
206 END Chance.