diff options
-rw-r--r-- | stdlib/random.ml | 12 | ||||
-rw-r--r-- | stdlib/random.mli | 12 |
2 files changed, 23 insertions, 1 deletions
diff --git a/stdlib/random.ml b/stdlib/random.ml index 1cd238999..a3300ad4a 100644 --- a/stdlib/random.ml +++ b/stdlib/random.ml @@ -92,6 +92,18 @@ external random_seed: unit -> int = "sys_random_seed";; let self_init () = init (random_seed());; + +(* Manipulating the current state. *) + +type state = { state_array : int array; j : int };; + +let get_state () = { state_array = state; j = !j };; + +let set_state s = + Array.blit s.state_array 0 state 0 (Array.length state); + j := s.j; +;; + (******************** (* Test functions. Not included in the library. diff --git a/stdlib/random.mli b/stdlib/random.mli index 54ecd688a..2b00e58c3 100644 --- a/stdlib/random.mli +++ b/stdlib/random.mli @@ -12,7 +12,7 @@ (* $Id$ *) -(* Module [Random]: pseudo-random number generator *) +(* Module [Random]: pseudo-random number generator (PRNG) *) val init : int -> unit (* Initialize the generator, using the argument as a seed. @@ -34,3 +34,13 @@ val float : float -> float between 0 (inclusive) and [bound] (exclusive). If [bound] is negative, the result is negative. If [bound] is 0, the result is 0. *) + +type state;; + (* Values of this type are used to store the current state of the + generator. *) +val get_state : unit -> state;; + (* Returns the current state of the generator. This is useful for + checkpointing computations that use the PRNG. *) +val set_state : state -> unit;; + (* Resets the state of the generator to some previous state returned by + [get_state]. *) |