Updated documentation of Random.self_init and Random.float.

Faster implementation of Random.float: to get a 53-bit random mantissa, combining two calls to Random.bits is enough, three was overkill.


git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@12262 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
master
Xavier Leroy 2012-03-23 09:58:22 +00:00
parent 875aab099e
commit 90fde3e40e
3 changed files with 13 additions and 9 deletions

View File

@ -57,8 +57,10 @@ Standard library:
with user-provided hash functions.
- Marshal: marshalling of function values (flag Marshal.Closures) now
also works for functions that come from dynamically-loaded modules (PR#5215)
- Random: more random initialization (Random.self_init()), using /dev/urandom
when available (e.g. Linux, FreeBSD, MacOS X, Solaris)
- Random:
. More random initialization (Random.self_init()), using /dev/urandom
when available (e.g. Linux, FreeBSD, MacOS X, Solaris)
. Faster implementation of Random.float
- Scanf: new function "unescaped" (PR#3888)
- Set and Map: more efficient implementation of "filter" and "partition"
- String: new function "map" (PR#3888)

View File

@ -130,13 +130,12 @@ module State = struct
else fun s bound -> Int64.to_nativeint (int64 s (Int64.of_nativeint bound))
;;
(* Returns a float 0 <= x < 1 with at most 90 bits of precision. *)
(* Returns a float 0 <= x <= 1 with at most 60 bits of precision. *)
let rawfloat s =
let scale = 1073741824.0
and r0 = Pervasives.float (bits s)
let scale = 1073741824.0 (* 2^30 *)
and r1 = Pervasives.float (bits s)
and r2 = Pervasives.float (bits s)
in ((r0 /. scale +. r1) /. scale +. r2) /. scale
in (r1 /. scale +. r2) /. scale
;;
let float s bound = rawfloat s *. bound;;

View File

@ -25,8 +25,11 @@ val full_init : int array -> unit
(** Same as {!Random.init} but takes more data as seed. *)
val self_init : unit -> unit
(** Initialize the generator with a more-or-less random seed chosen
in a system-dependent way. *)
(** 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). *)
val bits : unit -> int
(** Return 30 random bits in a nonnegative integer.
@ -53,7 +56,7 @@ val int64 : Int64.t -> Int64.t;;
val float : float -> float
(** [Random.float bound] returns a random floating-point number
between 0 (inclusive) and [bound] (exclusive). If [bound] is
between 0 and [bound] (inclusive). If [bound] is
negative, the result is negative or zero. If [bound] is 0,
the result is 0. *)