1995-10-16 05:40:46 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-10-16 05:40:46 -07:00
|
|
|
(* *)
|
|
|
|
(* Damien Doligez, projet Para, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-10-16 05:40:46 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
2003-06-12 04:15:26 -07:00
|
|
|
(** Pseudo-random number generators (PRNG). *)
|
|
|
|
|
2003-06-24 02:50:23 -07:00
|
|
|
(** {6 Basic functions} *)
|
1995-10-16 05:40:46 -07:00
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val init : int -> unit
|
2001-10-26 15:38:48 -07:00
|
|
|
(** Initialize the generator, using the argument as a seed.
|
1995-10-16 05:40:46 -07:00
|
|
|
The same seed will always yield the same sequence of numbers. *)
|
2001-10-26 15:38:48 -07:00
|
|
|
|
1995-10-16 05:40:46 -07:00
|
|
|
val full_init : int array -> unit
|
2001-12-03 14:16:03 -08:00
|
|
|
(** Same as {!Random.init} but takes more data as seed. *)
|
2001-10-26 15:38:48 -07:00
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val self_init : unit -> unit
|
2012-03-23 02:58:22 -07:00
|
|
|
(** Initialize the generator with a random seed chosen
|
|
|
|
in a system-dependent way. If [/dev/urandom] is available on
|
|
|
|
the host machine, it is used to provide a highly random initial
|
|
|
|
seed. Otherwise, a less random seed is computed from system
|
|
|
|
parameters (current time, process IDs). *)
|
1995-10-16 05:40:46 -07:00
|
|
|
|
1996-11-06 08:54:46 -08:00
|
|
|
val bits : unit -> int
|
2010-05-21 11:30:12 -07:00
|
|
|
(** Return 30 random bits in a nonnegative integer.
|
|
|
|
@before 3.12.0 used a different algorithm (affects all the following
|
|
|
|
functions)
|
|
|
|
*)
|
2001-10-26 15:38:48 -07:00
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val int : int -> int
|
2001-10-26 15:38:48 -07:00
|
|
|
(** [Random.int bound] returns a random integer between 0 (inclusive)
|
2007-02-09 05:31:15 -08:00
|
|
|
and [bound] (exclusive). [bound] must be greater than 0 and less
|
2001-10-29 09:58:08 -08:00
|
|
|
than 2{^30}. *)
|
2001-10-26 15:38:48 -07:00
|
|
|
|
2003-06-12 09:49:32 -07:00
|
|
|
val int32 : Int32.t -> Int32.t;;
|
|
|
|
(** [Random.int32 bound] returns a random integer between 0 (inclusive)
|
|
|
|
and [bound] (exclusive). [bound] must be greater than 0. *)
|
|
|
|
|
|
|
|
val nativeint : Nativeint.t -> Nativeint.t;;
|
|
|
|
(** [Random.nativeint bound] returns a random integer between 0 (inclusive)
|
|
|
|
and [bound] (exclusive). [bound] must be greater than 0. *)
|
|
|
|
|
|
|
|
val int64 : Int64.t -> Int64.t;;
|
|
|
|
(** [Random.int64 bound] returns a random integer between 0 (inclusive)
|
|
|
|
and [bound] (exclusive). [bound] must be greater than 0. *)
|
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
val float : float -> float
|
2001-10-26 15:38:48 -07:00
|
|
|
(** [Random.float bound] returns a random floating-point number
|
2012-03-23 02:58:22 -07:00
|
|
|
between 0 and [bound] (inclusive). If [bound] is
|
2003-06-12 04:15:26 -07:00
|
|
|
negative, the result is negative or zero. If [bound] is 0,
|
|
|
|
the result is 0. *)
|
2000-11-20 06:20:03 -08:00
|
|
|
|
2002-02-01 04:24:44 -08:00
|
|
|
val bool : unit -> bool
|
|
|
|
(** [Random.bool ()] returns [true] or [false] with probability 0.5 each. *)
|
|
|
|
|
2003-06-12 04:15:26 -07:00
|
|
|
|
2003-06-24 02:50:23 -07:00
|
|
|
(** {6 Advanced functions} *)
|
2003-06-12 04:15:26 -07:00
|
|
|
|
2003-06-24 02:50:23 -07:00
|
|
|
(** The functions from module [State] manipulate the current state
|
2012-04-13 05:44:29 -07:00
|
|
|
of the random generator explicitly.
|
2003-06-24 02:50:23 -07:00
|
|
|
This allows using one or several deterministic PRNGs,
|
2003-06-12 04:15:26 -07:00
|
|
|
even in a multi-threaded program, without interference from
|
2003-06-19 11:14:52 -07:00
|
|
|
other parts of the program.
|
2003-06-12 04:15:26 -07:00
|
|
|
*)
|
|
|
|
|
2003-06-12 09:49:32 -07:00
|
|
|
module State : sig
|
|
|
|
type t
|
|
|
|
(** The type of PRNG states. *)
|
2003-06-12 04:15:26 -07:00
|
|
|
|
2003-06-12 09:49:32 -07:00
|
|
|
val make : int array -> t
|
|
|
|
(** Create a new state and initialize it with the given seed. *)
|
2003-06-12 04:15:26 -07:00
|
|
|
|
2003-06-12 09:49:32 -07:00
|
|
|
val make_self_init : unit -> t
|
|
|
|
(** Create a new state and initialize it with a system-dependent
|
|
|
|
low-entropy seed. *)
|
|
|
|
|
|
|
|
val copy : t -> t
|
|
|
|
(** Return a copy of the given state. *)
|
|
|
|
|
|
|
|
val bits : t -> int
|
|
|
|
val int : t -> int -> int
|
|
|
|
val int32 : t -> Int32.t -> Int32.t
|
|
|
|
val nativeint : t -> Nativeint.t -> Nativeint.t
|
|
|
|
val int64 : t -> Int64.t -> Int64.t
|
|
|
|
val float : t -> float -> float
|
|
|
|
val bool : t -> bool
|
|
|
|
(** These functions are the same as the basic functions, except that they
|
|
|
|
use (and update) the given PRNG state instead of the default one.
|
|
|
|
*)
|
|
|
|
end;;
|
|
|
|
|
|
|
|
|
|
|
|
val get_state : unit -> State.t
|
|
|
|
(** Return the current state of the generator used by the basic functions. *)
|
|
|
|
|
|
|
|
val set_state : State.t -> unit
|
|
|
|
(** Set the state of the generator used by the basic functions. *)
|