Module random

Types

TMersenneTwister* = object 
  state: TMTState
  bytes_it: iterator (self: var TMersenneTwister): uint8 {.closure.}
Mersenne Twister (MT19937). Based on http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
TSystemRandom* = object 
  bytes_it: iterator (self: var TSystemRandom): uint8 {.closure.}
Random number generator based on bytes provided by the operating system's cryptographic source (see urandom)

Vars

mersenne_twister_inst* = init_MersenneTwister()
A global instance of MT used by the alias functions. seed() is called on it when the module is imported

Procs

proc urandom*(size: Natural): seq[uint8] {.raises: [EOS, EOutOfMemory], inline.}
Returns a seq of random integers 0 <= n < 256 provided by the operating system's cryptographic source (see posix_urandom, windows_urandom)
proc random_byte*[RNG](self: var RNG): uint8
Returns a uniformly distributed random integer 0 <= n < 256
proc random_int*[RNG](self: var RNG; max: Positive): Natural
Returns a uniformly distributed random integer 0 <= n < max
proc random*[RNG](self: var RNG): float64
Returns a uniformly distributed random number 0 <= n < 1
proc random_int*[RNG](self: var RNG; min, max: int): int
Returns a uniformly distributed random integer min <= n < max
proc random_int*[RNG](self: var RNG; slice: TSlice[int]): int {.inline.}
Returns a uniformly distributed random integer slice.a <= n <= slice.b
proc random_bool*[RNG](self: var RNG): bool {.inline.}
Returns a random boolean
proc random*[RNG](self: var RNG; min, max: float): float
Returns a uniformly distributed random number min <= n < max
proc random*[RNG](self: var RNG; max: float): float
Returns a uniformly distributed random number 0 <= n < max
proc random_choice*[RNG, T](self: var RNG; arr: T): auto {.inline.}
Selects a random element (all of them have an equal chance) from a 0-indexed random access container and returns it
proc shuffle*[RNG, T](self: var RNG; arr: var openarray[T])
Randomly shuffles elements of an array
proc random_byte*(self: var TMersenneTwister): uint8
proc random*(self: var TMersenneTwister): float64
proc init_MersenneTwister*(): TMersenneTwister
Initializes and returns a new TMersenneTwister
proc seed*(self: var TMersenneTwister; seed: int)
Seeds (randomizes) using 32 bits of an integer
proc seed*(self: var TMersenneTwister; seed: openarray[uint8])
Seeds (randomizes) using an array of bytes
proc seed*(self: var TMersenneTwister)
Seeds (randomizes) using an array of bytes provided by urandom, or, in case of failure, using the current time (with resolution of 1/256 sec)
proc random_byte*(self: var TSystemRandom): uint8
proc init_SystemRandom*(): TSystemRandom
Initializes and returns a new TSystemRandom
proc random_byte*(): uint8 {.inline.}
Alias to MT
proc random*(): float64 {.inline.}
Alias to MT
proc random*(max: float): float {.inline.}
Alias to MT
proc random*(min, max: float): float {.inline.}
Alias to MT
proc random_int*(max: Positive): Natural {.inline.}
Alias to MT
proc random_int*(min, max: int): int {.inline.}
Alias to MT
proc random_int*(slice: TSlice[int]): int {.inline.}
Alias to MT
proc random_bool*(): bool {.inline.}
Alias to MT
proc random_choice*[T](arr: T): auto {.inline.}
Alias to MT
proc shuffle*[T](arr: var openarray[T]) {.inline.}
Alias to MT

Iterators

iterator random_sample*[RNG, T](self: var RNG; arr: T; n: Natural): auto
Simple random sample. Yields n items randomly picked from a 0-indexed random access container arr, in the relative order they were in it. Each item has an equal chance to be picked and can be picked only once. Repeating items are allowed in arr, and they will not be treated in any special way. Raises EInvalidValue if there are less than n items in arr.
iterator random_sample*[T](arr: T; n: Natural): auto {.inline.}
Alias to MT
Generated: 2014-09-01 01:18:04 UTC