diff options
author | Xavier Leroy <xavier.leroy@inria.fr> | 2012-03-23 09:58:22 +0000 |
---|---|---|
committer | Xavier Leroy <xavier.leroy@inria.fr> | 2012-03-23 09:58:22 +0000 |
commit | 90fde3e40e13e0fbcd90e72ca43f481278566d60 (patch) | |
tree | 83893da10ebdf6f2d896f4dc072dc4c5d3105a69 | |
parent | 875aab099eacef1e89fc6cce349138ca50b09cce (diff) |
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
-rw-r--r-- | Changes | 6 | ||||
-rw-r--r-- | stdlib/random.ml | 7 | ||||
-rw-r--r-- | stdlib/random.mli | 9 |
3 files changed, 13 insertions, 9 deletions
@@ -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) diff --git a/stdlib/random.ml b/stdlib/random.ml index aa47536ba..50b570822 100644 --- a/stdlib/random.ml +++ b/stdlib/random.ml @@ -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;; diff --git a/stdlib/random.mli b/stdlib/random.mli index 389ef8d21..9c66c3a86 100644 --- a/stdlib/random.mli +++ b/stdlib/random.mli @@ -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. *) |