diff options
author | Damien Doligez <damien.doligez-inria.fr> | 2000-11-20 14:20:03 +0000 |
---|---|---|
committer | Damien Doligez <damien.doligez-inria.fr> | 2000-11-20 14:20:03 +0000 |
commit | 02d7534dfe57f0761fa9b9f20e69148ec7f027c4 (patch) | |
tree | 69591bf3d7df8fc3c4627e074ec27350562de081 /stdlib | |
parent | dbabdd47abe536925bfeef90d7ff0482be3c5410 (diff) |
random: ajout get/set_state
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@3336 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'stdlib')
-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]. *) |