<commit_before>/*  $Id$

    Part of SWI-Prolog

    Author:        <NAME>, <NAME>, <NAME>, <NAME>
    E-mail:        <EMAIL>
    WWW:           http://www.swi-prolog.org
    Copyright (C): Universidade do Porto, University of Amsterdam,
		   VU University Amsterdam.
*/

/*************************************************************************
*									 *
*	 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					 *
*									 *
*************************************************************************/

% original code from RA O'Keefe.

%   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.

:- module(random,
	  [ random/1,			% -float
	    random/3,			% +Low, +High, -Random
	    randseq/3,			% +Size, +Max, -Set
	    randset/3,			% +Size, +Max, -List
	    getrand/1,			% -State
	    setrand/1,			% +State
	    random_permutation/2	% +List, -Permutation
	  ]).
:- use_module(library(pairs)).
:- use_module(library(error)).

/** <module> Random numbers

This library is derived from the DEC10   library random. Later, the core
random generator was moved to C. The current version uses the SWI-Prolog
arithmetic functions to realise this library.  These functions are based
on the GMP library.

@copyright	DEC10 version: Public domain, YAP: Artistic
@author		<NAME>, <NAME>, <NAME>, <NAME>
@see		Built-in function random/1: A is random(10)
*/



		 /*******************************
		 *	   C PRIMITIVES		*
		 *******************************/

%%	random(-R:float) is det.
%
%	Binds R to a new random number in [0.0,1.0).
%
%	@see setrand/1, getrand/1 maye be used to fetch/set the state.
%	@see In SWI-Prolog, random/1 is implemented by the function
%	     random_float/0.

random(R) :-
	R is random_float.

%%	setrand(+State) is det.
%%	getrand(-State) is det.
%
%	Query/set the state of the random   generator.  This is intended
%	for  restarting  the  generator  at  a  known  state  only.  The
%	predicate  setrand/1  accepts  an  opaque    term   returned  by
%	getrand/1. This term may be  asserted,   written  and  read. The
%	application may not make other assumptions about this term.
%
%	For compatibility reasons with older   versions of this library,
%	setrand/1 also accepts a term rand(A,B,C), where  A, B and C are
%	integers in the range 1..30,000. This   argument is used to seed
%	the random generator.  Deprecated.
%
%	@see	set_random/1 and random_property/1 provide the SWI-Prolog
%		native implementation.
%	@error	existence_error(random_state, _) is raised if the
%		underlying infrastructure cannot fetch the random state.
%		This is currently the case if SWI-Prolog is not compiled
%		with the GMP library.

setrand(rand(A,B,C)) :- !,
	Seed is A<<30+B<<15+C,
	set_random(seed(Seed)).
setrand(State) :-
	set_random(state(State)).

:- if(current_predicate(random_property/1)).
getrand(State) :-
	random_property(state(State)).
:- else.
getrand(State) :-
	existence_error(random_state, State).
:- endif.


		 /*******************************
		 *	       PROLOG		*
		 *******************************/

%%	random(+L:int, +U:int, -R:int) is det.
%%	random(+L:float, +U:float, -R:float) is det.
%
%	Binds R to a random  number  in  [L,U).   If  L  and  U are both
%	integers, R is an integer, Otherwise, R  is a float. Note that U
%	will *never* be generated.

random(L, U, R) :-
	integer(L), integer(U), !,
	R is L+random(U-L).
random(L, U, R) :-
	number(L), number(U), !,
	R is L+((U-L)*random_float).
random(L, U, _) :-
	must_be(number, L),
	must_be(number, U).

%%	randset(+K:int, +N:int, -S:list(int)) is det.
%
%	S is a sorted list of K integers in the range 1..N.
%
%	@see randseq/3.


randset(K, N, S) :-
	must_be(nonneg, K),
	K =< N,
	randset(K, N, [], S).


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).


%%	randseq(+K:int, +N:int, -S:list(int)) is det.<commit_msg>ADDED: Some extensions to library(random).  (Incl. status).<commit_after>/*  $Id$

    Part of SWI-Prolog

    Author:        <NAME>, <NAME>, <NAME>, <NAME>
    E-mail:        <EMAIL>
    WWW:           http://www.swi-prolog.org
    Copyright (C): Universidade do Porto, University of Amsterdam,
		   VU University Amsterdam.
*/

/*************************************************************************
*									 *
*	 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					 *
*									 *
*************************************************************************/

% original code from RA O'Keefe.

%   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.

:- module(random,
	  [ random/1,			% -Float [0,1)
	    random/3,			% +Low, +High, -Random

	    getrand/1,			% -State
	    setrand/1,			% +State

	    maybe/0,
	    maybe/1,			% +P
	    maybe/2,			% +K, +N

	    random_perm2/4,		% A,B, X,Y

	    randseq/3,			% +Size, +Max, -Set
	    randset/3,			% +Size, +Max, -List
	    random_permutation/2	% +List, -Permutation
	  ]).
:- use_module(library(pairs)).
:- use_module(library(error)).

/** <module> Random numbers

This library is derived from the DEC10   library random. Later, the core
random generator was moved to C. The current version uses the SWI-Prolog
arithmetic functions to realise this library.  These functions are based
on the GMP library.

@copyright	DEC10 version: Public domain, YAP: Artistic
@author		<NAME>, <NAME>, <NAME>, <NAME>
@see		Built-in function random/1: A is random(10)
*/



		 /*******************************
		 *	   PRIMITIVES		*
		 *******************************/

%%	random(-R:float) is det.
%
%	Binds R to a new random number in [0.0,1.0).
%
%	@see setrand/1, getrand/1 maye be used to fetch/set the state.
%	@see In SWI-Prolog, random/1 is implemented by the function
%	     random_float/0.

random(R) :-
	R is random_float.

%%	random(+L:int, +U:int, -R:int) is det.
%%	random(+L:float, +U:float, -R:float) is det.
%
%	Binds R to a random  number  in  [L,U).   If  L  and  U are both
%	integers, R is an integer, Otherwise, R  is a float. Note that U
%	will *never* be generated.

random(L, U, R) :-
	integer(L), integer(U), !,
	R is L+random(U-L).
random(L, U, R) :-
	number(L), number(U), !,
	R is L+((U-L)*random_float).
random(L, U, _) :-
	must_be(number, L),
	must_be(number, U).


		 /*******************************
		 *	       STATE		*
		 *******************************/

%%	setrand(+State) is det.
%%	getrand(-State) is det.
%
%	Query/set the state of the random   generator.  This is intended
%	for  restarting  the  generator  at  a  known  state  only.  The
%	predicate  setrand/1  accepts  an  opaque    term   returned  by
%	getrand/1. This term may be  asserted,   written  and  read. The
%	application may not make other assumptions about this term.
%
%	For compatibility reasons with older   versions of this library,
%	setrand/1 also accepts a term rand(A,B,C), where  A, B and C are
%	integers in the range 1..30,000. This   argument is used to seed
%	the random generator.  Deprecated.
%
%	@see	set_random/1 and random_property/1 provide the SWI-Prolog
%		native implementation.
%	@error	existence_error(random_state, _) is raised if the
%		underlying infrastructure cannot fetch the random state.
%		This is currently the case if SWI-Prolog is not compiled
%		with the GMP library.

setrand(rand(A,B,C)) :- !,
	Seed is A<<30+B<<15+C,
	set_random(seed(Seed)).
setrand(State) :-
	set_random(state(State)).

:- if(current_predicate(random_property/1)).
getrand(State) :-
	random_property(state(State)).
:- else.
getrand(State) :-
	existence_error(random_state, State).
:- endif.


		 /*******************************
		 *	      MAYBE		*
		 *******************************/

%%	maybe is semidet.
%
%	Succeed/fail with equal probability (variant of maybe/1).

maybe :-
	random(2) =:= 0.

%%	maybe(+P) is semidet.
%
%	Succeed with probability P, fail with probability 1-P

maybe(P) :-
	must_be(between(0.0,1.0), P),
	random_float < P.

%%	maybe(K,N) is semidet.
%
%	Succeed with probability K/N (variant of maybe/1)

maybe(K, N) :-
	integer(K), integer(N),
	between(0, N, K), !,
	random(N) < K.
maybe(K, N) :-
	must_be(nonneg, K),
	must_be(nonneg, N),
	domain_error(not_less_than_zero,N-K).


		 /*******************************
		 *	    PERMUTATION		*
		 *******************************/

%%	random_perm2(?A, ?B, ?X, ?Y) is semidet.
%
%	Does X=A,Y=B or X=B,Y=A with equal probability.

random_perm2(A,B, X,Y) :-
	(   maybe
	->  X = A, Y = B
	;   X = B, Y = A
	).


		 /*******************************
		 *    SET AND LIST OPERATIONS	*
		 *******************************/

%%	randset(+K:int, +N:int, -S:list(int)) is det.
%
%	S is a sorted list of K integers in the range 1..N.
%
%	@see randseq/3.


randset(K, N, S) :-
	must_be(nonneg, K),
	K =< N,
	randset(K, N, [], S).


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).


%%	randseq(+K:int, +N:int, -S:list(int)) is det.
{'fraction_non_alphanumeric': 0.21838851216593538, 'fraction_numerical': 0.014260071798962904, 'mean_word_length': 3.0586806960744637, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2834219', 'n_tokens_mistral': 3983, 'n_tokens_neox': 3611, 'n_words': 1071}