Merge pull request #406 from notoria/random
Created the library random and updated library clpb
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
:- use_module(library(atts)).
|
||||
:- use_module(library(lists)).
|
||||
:- use_module(library(iso_ext)).
|
||||
:- use_module(library(random)).
|
||||
:- use_module(library(pairs)).
|
||||
:- use_module(library(dcgs)).
|
||||
:- use_module(library(error), []).
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
%% ?- use_module(library(iso_ext)).
|
||||
|
||||
:- module(iso_ext, [bb_b_put/2, bb_get/2, bb_put/2, call_cleanup/2,
|
||||
call_with_inference_limit/3, forall/2, maybe/0,
|
||||
call_with_inference_limit/3, forall/2,
|
||||
partial_string/1, partial_string/3,
|
||||
partial_string_tail/2, set_random/1,
|
||||
setup_call_cleanup/3, variant/2]).
|
||||
partial_string_tail/2, setup_call_cleanup/3,
|
||||
variant/2]).
|
||||
|
||||
forall(Generate, Test) :-
|
||||
\+ (Generate, \+ Test).
|
||||
@@ -144,21 +144,6 @@ call_with_inference_limit(_, _, R, Bb, B) :-
|
||||
|
||||
variant(X, Y) :- '$variant'(X, Y).
|
||||
|
||||
% succeeds with probability 0.5.
|
||||
maybe :- '$maybe'.
|
||||
|
||||
set_random(Seed) :-
|
||||
( nonvar(Seed) ->
|
||||
( Seed = seed(S) ->
|
||||
( var(S) -> throw(error(instantiation_error, set_random/1))
|
||||
; integer(S) -> '$set_seed'(S)
|
||||
; throw(error(type_error(integer(S), set_random/1)))
|
||||
)
|
||||
)
|
||||
; throw(error(instantiation_error, set_random/1))
|
||||
).
|
||||
|
||||
|
||||
partial_string(String, L, L0) :-
|
||||
( String == [] ->
|
||||
L = L0
|
||||
|
||||
51
src/prolog/lib/random.pl
Normal file
51
src/prolog/lib/random.pl
Normal file
@@ -0,0 +1,51 @@
|
||||
:- module(random, [maybe/0, random/1, random_integer/3, set_random/1]).
|
||||
|
||||
% succeeds with probability 0.5.
|
||||
maybe :- '$maybe'.
|
||||
|
||||
% The higher the precision, the slower it gets.
|
||||
random_number_precision(64).
|
||||
|
||||
random(R) :-
|
||||
var(R),
|
||||
random_number_precision(N),
|
||||
rnd(N, R).
|
||||
|
||||
random_integer(Lower, Upper, R) :-
|
||||
var(R),
|
||||
( (var(Lower) ; var(Upper)) ->
|
||||
throw(error(instantiation_error, random_integer/3))
|
||||
; \+ integer(Lower) ->
|
||||
throw(error(domain_error(integer, Lower), random_integer/3))
|
||||
; \+ integer(Upper) ->
|
||||
throw(error(domain_error(integer, Upper), random_integer/3))
|
||||
; Upper > Lower,
|
||||
random(R0),
|
||||
R is floor((Upper - Lower) * R0 + Lower)
|
||||
).
|
||||
|
||||
rnd(N, R) :-
|
||||
rnd_(N, 0, R).
|
||||
|
||||
rnd_(0, R, R) :- !.
|
||||
rnd_(N, R0, R) :-
|
||||
maybe,
|
||||
!,
|
||||
N1 is N - 1,
|
||||
rnd_(N1, R0, R).
|
||||
rnd_(N, R0, R) :-
|
||||
N1 is N - 1,
|
||||
R1 is R0 + 1.0 / 2.0 ^ N,
|
||||
rnd_(N1, R1, R).
|
||||
|
||||
set_random(Seed) :-
|
||||
( nonvar(Seed) ->
|
||||
( Seed = seed(S) ->
|
||||
( var(S) -> throw(error(instantiation_error, set_random/1))
|
||||
; integer(S) -> '$set_seed'(S)
|
||||
; throw(error(type_error(integer(S), set_random/1)))
|
||||
)
|
||||
)
|
||||
; throw(error(instantiation_error, set_random/1))
|
||||
).
|
||||
|
||||
Reference in New Issue
Block a user