Skip to content

Commit

Permalink
Add Random.int63
Browse files Browse the repository at this point in the history
  • Loading branch information
dra27 committed Apr 23, 2020
1 parent 0b82e2e commit dba5b25
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Changes
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Working version

### Standard library:

- #9487, #9489: Random.int now accepts 62-bit bounds on 64-bit systems.
- #9487, #9489: Add Random.int63 which allows 62-bit bounds on 64-bit systems.
(David Allsopp, request by Francois Berenger, review by ??)

### Other libraries:
Expand Down
10 changes: 8 additions & 2 deletions stdlib/random.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ module State = struct
let v = r mod n in
if r - v > 0x3FFFFFFF - n + 1 then intaux s n else v

let int s bound =
if bound > 0x3FFFFFFF || bound <= 0
then invalid_arg "Random.int"
else intaux s bound

let rec int63aux s n =
let b1 = bits s in
let b2 = bits s lsl 30 in
Expand All @@ -96,9 +101,9 @@ module State = struct
let v = r mod n in
if r - v > max_int - n + 1 then int63aux s n else v

let int s bound =
let int63 s bound =
if bound <= 0 then
invalid_arg "Random.int"
invalid_arg "Random.int63"
else if bound > 0x3FFFFFFF then
int63aux s bound
else
Expand Down Expand Up @@ -176,6 +181,7 @@ let default = {

let bits () = State.bits default
let int bound = State.int default bound
let int63 bound = State.int63 default bound
let int32 bound = State.int32 default bound
let nativeint bound = State.nativeint default bound
let int64 bound = State.int64 default bound
Expand Down
11 changes: 8 additions & 3 deletions stdlib/random.mli
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ val bits : unit -> int

val int : int -> int
(** [Random.int bound] returns a random integer between 0 (inclusive)
and [bound] (exclusive). [bound] must be greater than 0 and less
than {!Stdlib.max_int}
and [bound] (exclusive). [bound] must be greater than 0 and less
than 2{^30}. *)

@before 4.12.0 [bound] had to be less than 2{^30}. *)
val int63 : int -> int
(** [Random.int63 bound] is identical to {!int} if [bound] is less than 2{^30},
however [bound] may be any positive [int].
@since 4.12.0 *)

val int32 : Int32.t -> Int32.t
(** [Random.int32 bound] returns a random integer between 0 (inclusive)
Expand Down Expand Up @@ -91,6 +95,7 @@ module State : sig

val bits : t -> int
val int : t -> int -> int
val int63 : t -> int -> int
val int32 : t -> Int32.t -> Int32.t
val nativeint : t -> Nativeint.t -> Nativeint.t
val int64 : t -> Int64.t -> Int64.t
Expand Down

0 comments on commit dba5b25

Please sign in to comment.