2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
:- object(random,
|
|
|
|
implements(randomp)).
|
|
|
|
|
|
|
|
|
|
|
|
:- info([
|
2002-08-26 17:45:50 +01:00
|
|
|
version is 1.1,
|
2003-02-05 00:15:28 +00:00
|
|
|
author is 'Paulo Moura',
|
2002-08-26 17:45:50 +01:00
|
|
|
date is 2002/8/7,
|
2001-06-06 20:40:57 +01:00
|
|
|
comment is 'Random number generator predicates.']).
|
|
|
|
|
|
|
|
|
2002-08-26 17:45:50 +01:00
|
|
|
:- initialization(::reset_seed).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
:- private(seed_/3).
|
|
|
|
:- dynamic(seed_/3).
|
|
|
|
|
2002-08-26 17:45:50 +01:00
|
|
|
:- mode(seed_(-integer, -integer, -integer), one).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
:- info(seed_/3, [
|
|
|
|
comment is 'Stores the current random generator seed values.',
|
|
|
|
argnames is ['S0', 'S1', 'S2']]).
|
|
|
|
|
|
|
|
|
|
|
|
random(Random) :-
|
2002-08-26 17:45:50 +01:00
|
|
|
::retract(seed_(A0, A1, A2)),
|
2001-06-06 20:40:57 +01:00
|
|
|
random(A0, A1, A2, B0, B1, B2, Random),
|
2002-08-26 17:45:50 +01:00
|
|
|
::asserta(seed_(B0, B1, B2)).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
random(A0, A1, A2, B0, B1, B2, Random) :-
|
|
|
|
B0 is (A0*171) mod 30269,
|
|
|
|
B1 is (A1*172) mod 30307,
|
|
|
|
B2 is (A2*170) mod 30323,
|
|
|
|
Float is A0/30269 + A1/30307 + A2/30323,
|
|
|
|
Random is Float - truncate(Float).
|
|
|
|
|
|
|
|
|
|
|
|
random(Lower, Upper, Random) :-
|
|
|
|
integer(Lower),
|
|
|
|
integer(Upper),
|
|
|
|
Upper >= Lower,
|
|
|
|
!,
|
|
|
|
random(Float),
|
|
|
|
Random is truncate((Float*(Upper-Lower)+Lower)).
|
|
|
|
|
|
|
|
|
|
|
|
random(Lower, Upper, Random) :-
|
|
|
|
float(Lower),
|
|
|
|
float(Upper),
|
|
|
|
Upper >= Lower,
|
|
|
|
random(Float),
|
|
|
|
Random is Float*(Upper-Lower)+Lower.
|
|
|
|
|
|
|
|
|
|
|
|
randseq(Length, Lower, Upper, Sequence) :-
|
|
|
|
integer(Length),
|
|
|
|
Length >= 0,
|
|
|
|
integer(Lower),
|
|
|
|
integer(Upper),
|
|
|
|
Upper >= Lower,
|
|
|
|
!,
|
2002-08-26 17:45:50 +01:00
|
|
|
::retract(seed_(A0, A1, A2)),
|
2001-06-06 20:40:57 +01:00
|
|
|
randseq(Length, Lower, Upper, (A0, A1, A2), (B0, B1, B2), [], List),
|
2002-08-26 17:45:50 +01:00
|
|
|
::asserta(seed_(B0, B1, B2)),
|
2001-06-06 20:40:57 +01:00
|
|
|
map_truncate(List, Sequence).
|
|
|
|
|
|
|
|
randseq(Length, Lower, Upper, Sequence) :-
|
|
|
|
integer(Length),
|
|
|
|
Length >= 0,
|
|
|
|
float(Lower),
|
|
|
|
float(Upper),
|
|
|
|
Upper >= Lower,
|
2002-08-26 17:45:50 +01:00
|
|
|
::retract(seed_(A0, A1, A2)),
|
2001-06-06 20:40:57 +01:00
|
|
|
randseq(Length, Lower, Upper, (A0, A1, A2), (B0, B1, B2), [], Sequence),
|
2002-08-26 17:45:50 +01:00
|
|
|
::asserta(seed_(B0, B1, B2)).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
randseq(0, _, _, Seed, Seed, List, List) :-
|
|
|
|
!.
|
|
|
|
|
|
|
|
randseq(N, Lower, Upper, (A0, A1, A2), (C0, C1, C2), Acc, List) :-
|
|
|
|
N2 is N - 1,
|
|
|
|
random(A0, A1, A2, B0, B1, B2, R),
|
|
|
|
Random is R*(Upper-Lower)+Lower,
|
|
|
|
randseq(N2, Lower, Upper, (B0, B1, B2), (C0, C1, C2), [Random| Acc], List).
|
|
|
|
|
|
|
|
|
|
|
|
map_truncate([], []).
|
|
|
|
|
|
|
|
map_truncate([Float| Floats], [Integer| Integers]) :-
|
|
|
|
Integer is truncate(Float),
|
|
|
|
map_truncate(Floats, Integers).
|
|
|
|
|
|
|
|
|
|
|
|
randset(Length, Lower, Upper, Set) :-
|
|
|
|
integer(Length),
|
|
|
|
Length >= 0,
|
|
|
|
integer(Lower),
|
|
|
|
integer(Upper),
|
|
|
|
Upper >= Lower,
|
|
|
|
Length =< Upper - Lower,
|
|
|
|
!,
|
2002-08-26 17:45:50 +01:00
|
|
|
::retract(seed_(A0, A1, A2)),
|
2001-06-06 20:40:57 +01:00
|
|
|
randset(Length, Lower, Upper, (A0, A1, A2), (B0, B1, B2), [], Set),
|
2002-08-26 17:45:50 +01:00
|
|
|
::asserta(seed_(B0, B1, B2)).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
randset(Length, Lower, Upper, Set) :-
|
|
|
|
integer(Length),
|
|
|
|
Length >= 0,
|
|
|
|
float(Lower),
|
|
|
|
float(Upper),
|
|
|
|
Upper >= Lower,
|
2002-08-26 17:45:50 +01:00
|
|
|
::retract(seed_(A0, A1, A2)),
|
2001-06-06 20:40:57 +01:00
|
|
|
randset(Length, Lower, Upper, (A0, A1, A2), (B0, B1, B2), [], Set),
|
2002-08-26 17:45:50 +01:00
|
|
|
::asserta(seed_(B0, B1, B2)).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
randset(0, _, _, Seed, Seed, List, List) :-
|
|
|
|
!.
|
|
|
|
|
|
|
|
randset(N, Lower, Upper, (A0, A1, A2), (C0, C1, C2), Acc, List) :-
|
|
|
|
N2 is N - 1,
|
|
|
|
random(A0, A1, A2, B0, B1, B2, Float),
|
|
|
|
Float2 is Float*(Upper-Lower)+Lower,
|
|
|
|
(integer(Lower) ->
|
|
|
|
Random is truncate(Float2)
|
|
|
|
;
|
|
|
|
Random is Float2),
|
|
|
|
(not_member(Acc, Random) ->
|
|
|
|
add_ordered(Acc, Random, Acc2),
|
|
|
|
randset(N2, Lower, Upper, (B0, B1, B2), (C0, C1, C2), Acc2, List)
|
|
|
|
;
|
|
|
|
randset(N, Lower, Upper, (B0, B1, B2), (C0, C1, C2), Acc, List)).
|
|
|
|
|
|
|
|
|
|
|
|
not_member([], _).
|
|
|
|
|
|
|
|
not_member([H| T], R) :-
|
|
|
|
H =\= R,
|
|
|
|
not_member(T, R).
|
|
|
|
|
|
|
|
|
|
|
|
add_ordered([], R, [R]).
|
|
|
|
|
|
|
|
add_ordered([H| T], R, L) :-
|
|
|
|
H > R ->
|
|
|
|
L = [R, H| T]
|
|
|
|
;
|
|
|
|
L = [H| T2],
|
|
|
|
add_ordered(T, R, T2).
|
|
|
|
|
|
|
|
|
|
|
|
reset_seed :-
|
2002-08-26 17:45:50 +01:00
|
|
|
::retractall(seed_(_, _, _)),
|
|
|
|
::asserta(seed_(3172, 9814, 20125)).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
set_seed(Seed) :-
|
|
|
|
integer(Seed),
|
|
|
|
Seed > 0,
|
2002-08-26 17:45:50 +01:00
|
|
|
::retractall(seed_(_, _, _)),
|
2001-06-06 20:40:57 +01:00
|
|
|
S0 is Seed mod 30269,
|
|
|
|
S1 is Seed mod 30307,
|
|
|
|
S2 is Seed mod 30323,
|
2002-08-26 17:45:50 +01:00
|
|
|
::asserta(seed_(S0, S1, S2)).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
:- end_object.
|