| 1 |
(* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, |
| 2 |
2014, 2015, 2016, 2017 |
| 3 |
Free Software Foundation, Inc. *) |
| 4 |
(* This file is part of Chisel. |
| 5 |
|
| 6 |
Chisel is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License as published by the Free |
| 8 |
Software Foundation; either version 3, or (at your option) any later |
| 9 |
version. |
| 10 |
|
| 11 |
Chisel is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License along |
| 17 |
with gm2; see the file COPYING. If not, write to the Free Software |
| 18 |
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *) |
| 19 |
|
| 20 |
IMPLEMENTATION MODULE Chance ; |
| 21 |
|
| 22 |
|
| 23 |
FROM NumberIO IMPORT StrToCard ; |
| 24 |
FROM StrIO IMPORT WriteString, WriteLn ; |
| 25 |
|
| 26 |
(* FROM Random IMPORT RandomCard ; *) |
| 27 |
|
| 28 |
|
| 29 |
CONST |
| 30 |
MaxRand = 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 - non balanced calls to InitRandom/KillRandom in 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 MaxRand ; |
| 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 |
(* |
| 189 |
SetSeed - use, n, as the seed to pseudo random numbers. |
| 190 |
*) |
| 191 |
|
| 192 |
PROCEDURE SetSeed (n: CARDINAL) ; |
| 193 |
BEGIN |
| 194 |
RandomSeed := n |
| 195 |
END SetSeed ; |
| 196 |
|
| 197 |
|
| 198 |
(* |
| 199 |
GetSeed - return the seed used to create pseudo random numbers. |
| 200 |
*) |
| 201 |
|
| 202 |
PROCEDURE GetSeed () : CARDINAL ; |
| 203 |
BEGIN |
| 204 |
RETURN RandomSeed |
| 205 |
END GetSeed ; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
PROCEDURE Init ; |
| 210 |
BEGIN |
| 211 |
NoOfRandom := 0 ; |
| 212 |
NoOfIndices := 0 ; |
| 213 |
WITH RandomIndex[NoOfIndices] DO |
| 214 |
End := 0 |
| 215 |
END ; |
| 216 |
RandomSeed := 3 |
| 217 |
END Init ; |
| 218 |
|
| 219 |
|
| 220 |
BEGIN |
| 221 |
Init |
| 222 |
END Chance. |