Random.State(3)

✨ NAME ✨

okay so welcome to Random.State(3), straight out of the OCaml library manual. The official name line is Random.State β€” and the description is literally no description. That's not me skipping it, bestie, that's the whole assignment πŸ˜­πŸ’…

🧚 Module 🧚

The module is Random.State. That's it. That's the module. main character energy, no extra lore 🫢

πŸ’– Documentation πŸ’–

Inside the docs, it opens with this:

Module State : sig end

slay, a signature wrapper. and then we get the actual PRNG-state content, which is where it gets good ✨

  • type t β€” the type of PRNG states. short, mysterious, iconic.
  • val make : int array -> t β€” creates a new state and initializes it with the given seed. feed it an int array, get a state.
  • val make_self_init : unit -> t β€” creates a new state and initializes it with a random seed chosen in a system-dependent way. the seed is obtained as described in Random.self_init. so it's giving "system, you pick" 🌈
  • val copy : t -> t β€” returns a copy of the given state. duplicate the vibe, no cap.

Then the big group chat of random-draw functions. These are the same as the basic functions, except that they use (and update) the given PRNG state instead of the default one. That's the whole secret, and yes it's lowkey the most important line here πŸ”₯

  • val bits : t -> int
  • val int : t -> int -> int
  • val full_int : t -> int -> int
  • val int_in_range : t -> min:int -> max:int -> int
  • val int32 : t -> Int32.t -> Int32.t
  • val int32_in_range : t -> min:int32 -> max:int32 -> int32
  • val nativeint : t -> Nativeint.t -> Nativeint.t
  • val nativeint_in_range : t -> min:nativeint -> max:nativeint -> nativeint
  • val int64 : t -> Int64.t -> Int64.t
  • val int64_in_range : t -> min:int64 -> max:int64 -> int64
  • val float : t -> float -> float
  • val bool : t -> bool
  • val bits32 : t -> Int32.t
  • val bits64 : t -> Int64.t
  • val nativebits : t -> Nativeint.t
  • val split : t -> t β€” draws a fresh PRNG state from the given PRNG state. (the given PRNG state is modified.) the new PRNG is statistically independent from the given PRNG. data can be drawn from both PRNGs, in any order, without risk of correlation. both PRNGs can be split later, arbitrarily many times. Since 5.0. literally spawning independent little random universes πŸŒ€πŸ’–
  • val to_binary_string : t -> string β€” serializes the PRNG state into an immutable sequence of bytes. see Random.State.of_binary_string for deserialization. the string type is intended here for serialization only: the encoding is not human-readable and may not be printable. note that the serialization format may differ across OCaml versions. Since 5.1. it's giving "save the state, but make it bytes" πŸ”₯
  • val of_binary_string : string -> t β€” deserializes a byte sequence obtained by calling Random.State.to_binary_string. the resulting PRNG state will produce the same random numbers as the state that was passed as input to Random.State.to_binary_string. Since 5.1. raises Failure if the input is not in the expected format. note that the serialization format may differ across OCaml versions.
unlike the functions provided by the Marshal module, this function either produces a valid state or fails cleanly with a Failure exception. it can be safely used on user-provided, untrusted inputs. that's a big slay for safety πŸ”βœ¨

πŸ’– mwah πŸ’– this little page was brought to you by OCamldoc, dated 2026-06-22, and filed under Random.State(3). byeee πŸŽ€

πŸ“¦ where this page comes from