/* ========================================================================== */ 
/*                    PROVIDE MISCELLENAOUS RELATIVE TO RANDOM                */
/*                    ATTENTION DANS SWI set_random ne fonctionne qu'avec     */
/*                    la fonction random (X is random(N))                     */
/* ========================================================================== */

:- module(
          randomize,
          [
           init_randomize/0,
	　     init_randomize/1,
		   draw_float/1,
           draw_float/2,
           draw_float/3,
           draw_integer/2,
           draw_integer/3
          ]
          ).


:- use_module(library(random)).

max_number(10000).		  % use to simulate the drawn of a float number


/* =========================== INTIALIZE THE GENERATOR ====================== */

   /* initialize the random numbers generator with machine clock */
init_randomize :-
   get_time(T), 
   Time is round(10000 * float_fractional_part(T)),
   set_random(seed(Time)).

	/* idem with an offset */
init_randomize(Offset) :-
   get_time(T), 
   X is round(10000 * float_fractional_part(T)),
   Time is round(X + Offset),
   set_random(seed(Time)).


/* ============================ DRAW A FLOAT ================================ */

	/* draw a random float between 0 (included) and 1 (included) */
draw_float(X) :-
   max_number(Max),
   draw_integer(Max, Value),
   X is (Value * 1.0) / (Max * 1.0).
   

	/* draw a random float Value between 0 and Max (included) */
draw_float(Max, Value) :-
  draw_float(X),             % 0 <= X <= 1
  Value is X * Max.          % 0 <= Value <= Max
       

	/* draw a random float Value between Min (included) and Max (included) */
draw_float(Min, Max, Value) :-
  draw_float(X),                   % 0 <= X <= 1
  Value is Min + X * (Max-Min).    % Min <= Value <= Max


/* ============================ DRAW AN INTEGER ============================= */
       
	/* draw a random integer Value between 0 (included) and Max (included) */
draw_integer(Max, Value) :-
   K is Max+1,
   Value is random(K).    % Value is an integer in {0,..., Max} 

	/* draw a random integer Value between Min (included) and Max (included) */
draw_integer(Min, Max, Value) :-
   K is Max+1 - Min,
   N is random(K),    % N is an integer in {0, ..., Max-Min}
   Value is N + Min.  % Value is an integer in {Min, ..., Max}   


/* =========================== END OF LISTING =============================== */

