2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: random.yap *
|
|
|
|
* Last rev: 5/12/99 *
|
|
|
|
* mods: *
|
|
|
|
* comments: Random operations *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
/**
|
|
|
|
* @file random.yap
|
|
|
|
* @author original code from RA O'Keefe.
|
|
|
|
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
|
|
|
|
* @date Wed Nov 18 00:05:21 2015
|
2016-01-03 02:06:09 +00:00
|
|
|
*
|
2015-11-18 15:06:25 +00:00
|
|
|
* @brief Integer Random Number Generator
|
2016-01-03 02:06:09 +00:00
|
|
|
*
|
|
|
|
*
|
2015-11-18 15:06:25 +00:00
|
|
|
*/
|
2001-06-11 21:20:36 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
:- module(random, [
|
|
|
|
random/1,
|
|
|
|
random/3,
|
|
|
|
randseq/3,
|
|
|
|
randset/3,
|
|
|
|
getrand/1,
|
|
|
|
setrand/1
|
|
|
|
]).
|
|
|
|
|
|
|
|
/** @defgroup random Random Number Generator
|
2015-01-04 23:58:23 +00:00
|
|
|
@ingroup library
|
2014-09-11 20:06:57 +01:00
|
|
|
@{
|
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
Since YAP-4.3.19 YAP uses
|
2014-09-11 20:06:57 +01:00
|
|
|
the O'Keefe public-domain algorithm, based on the "Applied Statistics"
|
|
|
|
algorithm AS183.
|
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
The following random number operations are included with the
|
|
|
|
`use_module(library(random))` command.
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
In ROK's words: ``This is algorithm AS 183 from Applied Statistics. I also have a C
|
|
|
|
version. It is really very good. It is straightforward to make a
|
|
|
|
version which yields 15-bit random integers using only integer
|
|
|
|
arithmetic.''
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
/** @pred getrand(- _Key_)
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
Unify _Key_ with a term of the form `rand(X,Y,Z)` describing the
|
|
|
|
current state of the random number generator.
|
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/** @pred random(+ _LOW_, + _HIGH_, - _NUMBER_)
|
|
|
|
|
|
|
|
Unify _Number_ with a number in the range
|
|
|
|
`[LOW...HIGH)`. If both _LOW_ and _HIGH_ are
|
|
|
|
integers then _NUMBER_ will also be an integer, otherwise
|
|
|
|
_NUMBER_ will be a floating-point number.
|
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/** @defgroup Pseudo_Random Pseudo Random Number Integer Generator
|
2015-01-04 23:58:23 +00:00
|
|
|
@ingroup library
|
2014-09-11 20:06:57 +01:00
|
|
|
@{
|
|
|
|
|
|
|
|
The following routines produce random non-negative integers in the range
|
|
|
|
0 .. 2^(w-1) -1, where w is the word size available for integers, e.g.
|
|
|
|
32 for Intel machines and 64 for Alpha machines. Note that the numbers
|
|
|
|
generated by this random number generator are repeatable. This generator
|
|
|
|
was originally written by Allen Van Gelder and is based on Knuth Vol 2.
|
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
/** @pred random(- _Number_)
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
Unify _Number_ with a floating-point number in the range `[0...1)`.
|
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
2016-01-03 02:06:09 +00:00
|
|
|
/** @pred randseq(+ _LENGTH_, + _MAX_, - _Numbers_)
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
Unify _Numbers_ with a list of _LENGTH_ unique random integers
|
|
|
|
in the range `[1... _MAX_)`.
|
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
2016-01-03 02:06:09 +00:00
|
|
|
/** @pred randset(+ _LENGTH_, + _MAX_, - _Numbers_)
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
Unify _Numbers_ with an ordered list of _LENGTH_ unique random
|
|
|
|
integers in the range `[1... _MAX_)`.
|
|
|
|
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
2016-01-03 02:06:09 +00:00
|
|
|
/** @pred setrand(+ _Key_)
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
Use a term of the form `rand(X,Y,Z)` to set a new state for the
|
|
|
|
random number generator. The integer `X` must be in the range
|
|
|
|
`[1...30269)`, the integer `Y` must be in the range
|
|
|
|
`[1...30307)`, and the integer `Z` must be in the range
|
|
|
|
`[1...30323)`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2016-01-04 14:11:09 +00:00
|
|
|
%:- use_module(library(pairs)).
|
2012-02-13 09:39:09 +00:00
|
|
|
:- use_module(library(lists)).
|
|
|
|
|
|
|
|
|
2010-06-21 17:11:48 +01:00
|
|
|
:- load_foreign_files([yap_random], [], init_random).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
% random(R) binds R to a new random number in [0.0,1.0)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
% random(L, U, R) binds R to a random integer in [L,U)
|
|
|
|
% when L and U are integers (note that U will NEVER be generated),
|
|
|
|
% or to a random floating number in [L,U) otherwise.
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
random(L, U, R) :-
|
2012-02-13 09:39:09 +00:00
|
|
|
( integer(L), integer(U) ->
|
|
|
|
U > L,
|
|
|
|
random(X),
|
|
|
|
R is L+integer((U-L)*X)
|
|
|
|
;
|
|
|
|
number(L), number(U),
|
|
|
|
U > L,
|
|
|
|
random(X),
|
|
|
|
R is L+((U-L)*X)
|
|
|
|
).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
/* There are two versions of this operation.
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
randset(K, N, S)
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
generates a random set of K integers in the range 1..N.
|
|
|
|
The result is an ordered list, such as setof might produce.
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
randseq(K, N, L)
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
generates a random sequence of K integers, the order is as
|
|
|
|
random as we can make it.
|
|
|
|
*/
|
2016-01-03 02:06:09 +00:00
|
|
|
|
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
randset(K, N, S) :-
|
|
|
|
K >= 0,
|
|
|
|
K =< N,
|
|
|
|
randset(K, N, [], S).
|
2016-01-03 02:06:09 +00:00
|
|
|
|
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
randset(0, _, S, S) :- !.
|
|
|
|
randset(K, N, Si, So) :-
|
|
|
|
random(X),
|
|
|
|
X * N < K, !,
|
|
|
|
J is K-1,
|
|
|
|
M is N-1,
|
|
|
|
randset(J, M, [N|Si], So).
|
|
|
|
randset(K, N, Si, So) :-
|
|
|
|
M is N-1,
|
|
|
|
randset(K, M, Si, So).
|
2016-01-03 02:06:09 +00:00
|
|
|
|
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
randseq(K, N, S) :-
|
|
|
|
randseq(K, N, L, []),
|
|
|
|
keysort(L, R),
|
|
|
|
strip_keys(R, S).
|
2016-01-03 02:06:09 +00:00
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
randseq(0, _, S, S) :- !.
|
|
|
|
randseq(K, N, [Y-N|Si], So) :-
|
|
|
|
random(X),
|
|
|
|
X * N < K, !,
|
|
|
|
random(Y),
|
|
|
|
J is K-1,
|
|
|
|
M is N-1,
|
|
|
|
randseq(J, M, Si, So).
|
|
|
|
randseq(K, N, Si, So) :-
|
|
|
|
M is N-1,
|
|
|
|
randseq(K, M, Si, So).
|
2016-01-03 02:06:09 +00:00
|
|
|
|
|
|
|
|
2001-06-11 21:20:36 +01:00
|
|
|
strip_keys([], []) :- !.
|
|
|
|
strip_keys([_-K|L], [K|S]) :-
|
|
|
|
strip_keys(L, S).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
setrand(rand(X,Y,Z)) :-
|
|
|
|
integer(X),
|
|
|
|
integer(Y),
|
|
|
|
integer(Z),
|
2001-06-11 21:20:36 +01:00
|
|
|
X > 0,
|
|
|
|
X < 30269,
|
|
|
|
Y > 0,
|
2016-01-03 02:06:09 +00:00
|
|
|
Y < 30307,
|
2001-06-11 21:20:36 +01:00
|
|
|
Z > 0,
|
|
|
|
Z < 30323,
|
|
|
|
setrand(X,Y,Z).
|
|
|
|
|
2001-07-10 21:04:10 +01:00
|
|
|
getrand(rand(X,Y,Z)) :-
|
|
|
|
getrand(X,Y,Z).
|
2017-04-07 23:10:59 +01:00
|
|
|
/** @} */
|
|
|
|
|