This library provides portable random number generators and an abstraction over native backend Prolog compiler random number generator if available.
Open the ../../docs/library_index.html#random link in a web browser.
To load all entities in this library, load the
| ?- logtalk_load(random(loader)).
To test this library predicates, load the
| ?- logtalk_load(random(tester)).
random object implements portable random number generator and
supports multiple random number generators, using different seeds, by
defining derived objects. For example:
:- object(my_random_generator_1, extends(random)). :- initialization(::reset_seed). :- end_object.
fast_random object also implements a portable random number
generator but does not support deriving multiple random number
generators, which makes it a bit faster than the
fast_random objects manage the random number
generator seed using internal dynamic state. The predicates that update
the seed are declared as synchronized (when running on Prolog backends
that support threads). Still, care must be taken when using these
objects from multi-threaded applications as there is not portable
solution to protect seed updates from signals and prevent inconsistent
state when threads are canceled.
fast_random objects always initialize the random
generator seed to the same value, thus providing a pseudo random number
randomize/1 predicate can be used to initialize the
seed with a random value. The argument should be a large positive
integer. In alternative, when using a small integer argument, discard
the first dozen random values.
backend_random object abstracts the native backend Prolog
compiler random number generator for the basic
set_seed/1 predicates providing a portable
implementation for the remaining predicates. This makes the object
stateless, which can allow reliable use from multiple threads. Consult
the backend Prolog compiler documentation for details on its random
number generator properties. Note that several of the supported backend
Prolog systems, notably B-Prolog, CxProlog, ECLiPSe, JIProlog, and
Quintus Prolog, do not provide implementations for both the
set_seed/1 predicates and calling these
predicates simply succeed without performing any action.