Skip to content

Commit aa36f91

Browse files
committed
Further simplify Random.jump and improve its documentation
As suggested by @jhjourdan, it's simpler to take a copy of the current default state then jump it.
1 parent 10c6ff4 commit aa36f91

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

stdlib/random.ml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ module State = struct
137137

138138
end
139139

140-
let default_origin =
140+
let default =
141141
State.make
142142
[| 0b001001000011111101101010100010;
143143
0b001000010110100011000010001101;
@@ -149,7 +149,6 @@ let default_origin =
149149
0b111010100110001110110001001110 |]
150150
(* These are the first 240 binary digits of the fractional part of pi,
151151
as eight 30-bit integers. *)
152-
let default = State.copy default_origin
153152

154153
let bits () = State.bits default
155154
let int bound = State.int default bound
@@ -165,16 +164,14 @@ let nativebits () = State.nativebits default
165164

166165
(* Seeding *)
167166

168-
let full_init seed =
169-
State.reinit default_origin seed;
170-
State.assign default default_origin
171-
167+
let full_init seed = State.reinit default seed
172168
let init seed = full_init [| seed |]
173169
let self_init () = full_init (random_seed())
174170

175171
(* Generating a new independent state by jumping *)
176172

177-
let jump () = State.jump default_origin; State.copy default_origin
173+
let jump () =
174+
let st = State.copy default in State.jump default; st
178175

179176
(* Manipulating the current state. *)
180177

stdlib/random.mli

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,15 @@ val set_state : State.t -> unit
137137
(** Set the state of the generator used by the basic functions. *)
138138

139139
val jump : unit -> State.t
140-
(** Return a fresh PRNG state that is obtained by "jumping" from the
141-
initial state of the generator used by the default functions.
142-
"Jumping", here, means advancing the state as if a great many numbers
143-
(here, 2{^128} numbers) were drawn from this state.
144-
The state returned by {!Random.jump} is statistically independent
145-
from the state used by the default functions.
140+
(** Return a fresh PRNG state that is statistically independent
141+
from the state of the generator used by the basic functions.
146142
Data can be drawn from both states, in any order, without risk of
147-
correlation. *)
143+
correlation.
144+
Multiple calls to {!Random.jump} will return PRNG states that
145+
are pairwise statistically independent.
146+
This is implemented by returning a copy of the current state of the
147+
generator used by the basic functions, then jumping this generator,
148+
i.e. advancing it as if a great many numbers (here, 2{^128} numbers)
149+
were drawn from this generator.
150+
*)
151+

0 commit comments

Comments
 (0)