Module PRNG.Chacha.State

module State: PRNG.STATE 

type t 

The type of generators

Creating and seeding PRNGs

val seed : string -> t

Initialize a generator from the given seed. The seed is given as a character string. The length and randomness of the seed limit the total entropy of the generator. For example, 64 bits of entropy can be obtained by giving a seed consisting of 8 cryptographically-strong random characters (as obtained e.g. by reading /dev/random.

val make : int array -> t

Initialize a generator from the given seed. The seed is given as an array of integers.

val make_self_init : unit -> t

Initialize a generator from a random seed obtained from the operating system. Tries hard to provide at least 64 bits of entropy. With high probability, successive calls to make_self_init return different PRNGs with different seeds.

Generating pseudo-random data

val bool : t -> bool
val bit : t -> bool

Return a Boolean value in false,true with 0.5 probability each.

val uniform : t -> float

Return a floating-point number evenly distributed between 0.0 and 1.0. 0.0 and 1.0 are never returned. The result is of the form n * 2{^-53}, where n is a random integer in (0, 2{^53}).

val float : t -> float -> float

float g x returns a floating-point number evenly distributed between 0.0 and x. If x is negative, negative numbers between x and 0.0 are returned. Implemented as uniform g *. x. Consequently, the values 0.0 and x can be returned (as a result of floating-point rounding), but not if x is 1.0, since float g 1.0 behaves exactly like uniform g.

val byte : t -> int
val bits8 : t -> int

Return an 8-bit integer evenly distributed between 0 and 255.

val bits : t -> int
val bits30 : t -> int

Return a 30-bit integer evenly distributed between 0 and 230-1 (that is, 1073741823, or 0x3FFFFFFF).

val int : t -> int -> int

int g n returns an integer evenly distributed between 0 included and n excluded. Hence there are n possible return values with probability 1/n each. n must be greater than 0 and no greater than 230-1.

val bits32 : t -> int32

Return a 32-bit integer evenly distributed between and .

val int32 : t -> int32 -> int32

int32 g n returns a 32-bit integer evenly distributed between 0 included and n excluded. n must be strictly positive.

Note that int32 Int32.max_int produces numbers between 0 and Int32.max_int excluded. To produce numbers between 0 and Int32.max_int included, use Int32.logand (bits32 g) Int32.max_int.

val bits64 : t -> int64

Return a 64-bit integer evenly distributed between and .

val int64 : t -> int64 -> int64

int64 g n returns a 64-bit integer evenly distributed between 0 included and n excluded. n must be strictly positive.

Note that int64 Int64.max_int produces numbers between 0 and Int64.max_int excluded. To produce numbers between 0 and Int64.max_int included, use Int64.logand (bits64 g) Int64.max_int.

val nativebits : t -> nativeint

nativebits g returns a platform-native integer (32 or 64 bits) evenly distributed between and .

val nativeint : t -> nativeint -> nativeint

nativeint g n returns a platform-native integer between 0 included and n included. n must be strictly positive.

val char : t -> char

Return a character evenly distributed among '\000' ... '\255'.

val bytes : t -> bytes -> int -> int -> unit

bytes g b ofs len produces len bytes of pseudo-random data and stores them in byte sequence b at offsets ofs to ofs+len-1.

Raise Invalid_argument if len < 0 or ofs and len do not designate a valid range of b.

Splitting and copying

val split : t -> t

split g returns a fresh generator g' that is statistically independent from the current generator g. The two generators g and g' can be used in parallel and will produce independent pseudo-random data. Each generator g and g' can be splitted again in the future.

val copy : t -> t

copy g returns a generator g' that has the same state as g. The two generators g and g' produce the same data.

Reseeding

val reseed : t -> string -> unit

reseed g s reinitializes the generator g with fresh seed data from string s. This is like seed s except that the existing generator g is seeded, instead of a new generator being returned. It is good practice to reseed a PRNG after a certain quantity of pseudo-random data has been produced from it: typically 232 numbers for the PRNG.Splitmix generator and 264 bytes for then PRNG.Chacha generator.

val remake : t -> int array -> unit

remake g a reinitializes the generator g with fresh seed data from array a. This is like reseed except that the seed is given as an array of integers.