/* Simple random number generator.
Good for numbers in some range less than 100,000 long.
Called thus:
random(X, Lower, Upper)
where Lower and Upper must be instantiated to integers,
and X must be uninstantiated. Instantiates X to an integer
in the range Lower ... Upper-1.
Cobbled up by Peter Ross, 3 May 1983
*/
seed(27134, 9213, 17773).
getrand(X) :- seed(A,B,C),
A1 is (A*171) mod 30269,
B1 is (B*170) mod 30307,
C1 is (C*172) mod 30323,
retract(seed(A,B,C)),
asserta(seed(A1,B1,C1)),
X is (7*A + 11*B + 13*C) mod 100000.
random(X,Lower,Upper) :- (nonvar(X); var(Lower); var(Upper); Upper =< Lower),
!,
write('*** error in random(_,_,_) ***'), nl.
random(X,Lower,Upper) :- Range is Upper-Lower,
getrand(X1),
!,
X is (X1 mod Range) + Lower.
/* why the cut in random, you may ask? So that your program can backtrack
past random(_,_,_), and efficiently ...
*/