Skip to content

Commit

Permalink
Remove the unnecessary extra bit
Browse files Browse the repository at this point in the history
  • Loading branch information
dra27 committed Mar 15, 2021
1 parent efd6c45 commit 414ff8c
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions stdlib/random.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,24 @@ module State = struct
let max_int_32 = (1 lsl 30) + 0x3FFFFFFF in (* 0x7FFFFFFF *)
let b1 = bits s in
let b2 = bits s in
let (b, max_int) =
let (r, max_int) =
if n <= max_int_32 then
(* 32 random bits on both 64-bit OCaml and JavaScript.
Use upper 16 bits of b1 and b2. *)
(* 31 random bits on both 64-bit OCaml and JavaScript.
Use upper 15 bits of b1 and 16 bits of b2. *)
let bpos =
(((b2 land 0x3FFFC000) lsl 2) lor (b1 lsr 14))
(((b2 land 0x3FFFC000) lsl 1) lor (b1 lsr 15))
in
(bpos, max_int_32)
else
let b3 = bits s in
(* 63 random bits on 64-bit OCaml; unreachable on JavaScript.
Use upper 21 bits of b1, b2 and b3. *)
(* 62 random bits on 64-bit OCaml; unreachable on JavaScript.
Use upper 20 bits of b1 and 21 bits of b2 and b3. *)
let bpos =
((((b3 land 0x3FFFFE00) lsl 12) lor (b2 lsr 9)) lsl 21) lor (b1 lsr 9)
((((b3 land 0x3FFFFE00) lsl 12) lor (b2 lsr 9)) lsl 20)
lor (b1 lsr 10)
in
(bpos, max_int)
in
let r = b lsr 1 in (* Random non-negative int *)
let v = r mod n in
if r - v > max_int - n + 1 then int63aux s n else v

Expand Down

0 comments on commit 414ff8c

Please sign in to comment.