Source

papl / src / PaplRandom.ml

Full commit
(*
  Copyright (c) 2012 Anders Lau Olsen.
  See LICENSE file for terms and conditions.
*)
type rng_t = BatRandom.State.t option

module Helper = struct
  let random_enum clone next =
    let count () = raise BatEnum.Infinite_enum in
    let clone () = clone (BatRandom.get_state ()) in
      BatEnum.make ~next ~count ~clone

  let enum_bits () =
    let next = BatRandom.bits in
    let clone state = BatRandom.State.enum_bits state () in
      random_enum clone next

  let enum_int bound =
    let next () = BatRandom.int bound in
    let clone state = BatRandom.State.enum_int state bound in
      random_enum clone next

  let enum_int32 bound =
    let next () = BatRandom.int32 bound in
    let clone state = BatRandom.State.enum_int32 state bound in
      random_enum clone next

  let enum_int64 bound =
    let next () = BatRandom.int64 bound in
    let clone state = BatRandom.State.enum_int64 state bound in
      random_enum clone next

  let enum_float bound =
    let next () = BatRandom.float bound in
    let clone state = BatRandom.State.enum_float state bound in
      random_enum clone next

  let enum_nativeint bound =
    let next () = BatRandom.nativeint bound in
    let clone state = BatRandom.State.enum_nativeint state bound in
      random_enum clone next

  let enum_bool () =
    let next = BatRandom.bool in
    let clone state = BatRandom.State.enum_bool state () in
      random_enum clone next

  let enum_char () =
    let next = BatRandom.char in
    let clone state = BatRandom.State.enum_char state () in
      random_enum clone next
end

let enum_by ?(rng = None) default_fun state_fun =
  match rng with
      None -> default_fun
    | Some state -> fun arg -> state_fun state arg

let enum_bits ?rng =
  enum_by ?rng Helper.enum_bits BatRandom.State.enum_bits

let enum_int ?rng =
  enum_by ?rng Helper.enum_int BatRandom.State.enum_int

let enum_int32 ?rng =
  enum_by ?rng Helper.enum_int32 BatRandom.State.enum_int32

let enum_int64 ?rng =
  enum_by ?rng Helper.enum_int64 BatRandom.State.enum_int64

let enum_float ?rng =
  enum_by ?rng Helper.enum_float BatRandom.State.enum_float

let enum_nativeint ?rng =
  enum_by ?rng Helper.enum_nativeint BatRandom.State.enum_nativeint

let enum_bool ?rng =
  enum_by ?rng Helper.enum_bool BatRandom.State.enum_bool

let enum_char ?rng =
  enum_by ?rng Helper.enum_char BatRandom.State.enum_char

let get_by ?(rng = None) default_fun state_fun =
  match rng with
      None -> default_fun
    | Some state -> fun arg -> state_fun state arg

let int ?rng = get_by ?rng BatRandom.int BatRandom.State.int
let int32 ?rng = get_by ?rng BatRandom.int32 BatRandom.State.int32
let nativeint ?rng = get_by ?rng BatRandom.nativeint BatRandom.State.nativeint
let int64 ?rng = get_by ?rng BatRandom.int64 BatRandom.State.int64
let float ?rng = get_by ?rng BatRandom.float BatRandom.State.float

let bits ?(rng = None) =
  match rng with
      None -> BatRandom.bits
    | Some state -> fun () -> BatRandom.State.bits state

let bool ?(rng = None) =
  match rng with
      None -> BatRandom.bool
    | Some state -> fun () -> BatRandom.State.bool state