This module is exported by all other modules. It defines common operations that work for all the PRNGs provided by this library.
Procs
proc randomInt[RNG, ](rng: var RNG; T: typedesc[SomeInteger]): T:type {.inline.}
- Returns a uniformly distributed random integer T.low <= x <= T.high Source
proc randomByte[RNG](rng: var RNG): uint8 {.inline, deprecated.}
-
Returns a uniformly distributed random integer 0 <= x < 256
Deprecated: Use randomInt(uint8) instead.
Source proc randomInt[RNG](rng: var RNG; max: Positive): Natural {.inline.}
- Returns a uniformly distributed random integer 0 <= x < max Source
proc randomInt[RNG](rng: var RNG; min, max: int): int {.inline.}
- Returns a uniformly distributed random integer min <= x < max Source
proc randomInt[RNG](rng: var RNG; range: Slice[int]): int {.inline.}
- Returns a uniformly distributed random integer range.a <= x <= range.b Source
proc randomBool[RNG](rng: var RNG): bool {.inline.}
- Returns a random boolean Source
proc random[RNG](rng: var RNG): float64
- Returns a uniformly distributed random number 0 <= x < 1 Source
proc random[RNG](rng: var RNG; max: float): float {.inline.}
- Returns a uniformly distributed random number 0 <= x < max Source
proc random[RNG](rng: var RNG; min, max: float): float {.inline.}
- Returns a uniformly distributed random number min <= x < max Source
proc randomPrecise[RNG](rng: var RNG): float64
-
Returns a uniformly distributed random number 0 <= x <= 1, with more resolution (doesn't skip values).
Based on http://mumble.net/~campbell/2014/04/28/uniform-random-float
Source proc randomChoice[RNG, RAContainer](rng: var RNG; arr: RAContainer): auto {. inline.}
- Selects a random element (all of them have an equal chance) from a random access container and returns it Source
proc shuffle[RNG, RAContainer](rng: var RNG; arr: var RAContainer)
- Randomly shuffles elements of a random access container. Source
proc randomSample[T, RNG](rng: var RNG; iter: iterator (): T; n: Natural): seq[T]
-
Random sample using reservoir sampling algorithm.
Returns a sequence of n items randomly picked from an iterator iter, in no particular order. Each item has an equal chance to be picked and can be picked only once. Repeating items are allowed in iter, and they will not be treated in any special way.
Raises ValueError if there are less than n items in iter.
Source
Iterators
iterator randomSample[RNG](rng: var RNG; range: Slice[int]; n: Natural): int
-
Yields n random integers range.a <= x <= range.b in random order. Each number has an equal chance to be picked and can be picked only once.
Raises ValueError if there are less than n items in range.
Source iterator randomSample[RNG, RAContainer](rng: var RNG; arr: RAContainer; n: Natural): auto
-
Yields n items randomly picked from a random access container arr, in random order. Each item has an equal chance to be picked and can be picked only once. Duplicate items are allowed in arr, and they will not be treated in any special way.
Raises ValueError if there are less than n items in arr.
Source