random

Nim's standard random number generator. Based on the xoroshiro128+ (xor/rotate/shift/rotate) library.

Do not use this module for cryptographic purposes!

Types

Rand = object
  a0, a1: ui
State of the random number generator. The procs that use the default state are not thread-safe!   Source Edit

Procs

proc next(r: var Rand): uint64 {...}{.raises: [], tags: [].}
Uses the state to compute a new uint64 random number.   Source Edit
proc skipRandomNumbers(s: var Rand) {...}{.raises: [], tags: [].}
This is the jump function for the generator. It is equivalent to 2^64 calls to next(); it can be used to generate 2^64 non-overlapping subsequences for parallel computations.   Source Edit
proc random(max: int): int {...}{.gcsafe, locks: 0, deprecated, raises: [], tags: [].}
Returns a random number in the range 0..max-1. The sequence of random number is always the same, unless randomize is called which initializes the random number generator with a "random" number, i.e. a tickcount. Deprecated since version 0.18.0. Use rand instead.   Source Edit
proc random(max: float): float {...}{.gcsafe, locks: 0, deprecated, raises: [], tags: [].}
Returns a random number in the range 0..<max. The sequence of random number is always the same, unless randomize is called which initializes the random number generator with a "random" number, i.e. a tickcount. Deprecated since version 0.18.0. Use rand instead.   Source Edit
proc random[T](x: HSlice[T, T]): T {...}{.deprecated.}
For a slice a .. b returns a value in the range a .. b-1. Deprecated since version 0.18.0. Use rand instead.   Source Edit
proc random[T](a: openArray[T]): T {...}{.deprecated.}
returns a random element from the openarray a. Deprecated since version 0.18.0. Use rand instead.   Source Edit
proc rand(r: var Rand; max: Natural): int {...}{.gcsafe, locks: 0, raises: [], tags: [].}
Returns a random number in the range 0..max. The sequence of random number is always the same, unless randomize is called which initializes the random number generator with a "random" number, i.e. a tickcount.   Source Edit
proc rand(max: int): int {...}{.gcsafe, locks: 0, raises: [], tags: [].}
Returns a random number in the range 0..max. The sequence of random number is always the same, unless randomize is called which initializes the random number generator with a "random" number, i.e. a tickcount.   Source Edit
proc rand(r: var Rand; max: range[0.0 .. high(float)]): float {...}{.gcsafe, locks: 0,
    raises: [], tags: [].}
Returns a random number in the range 0..max. The sequence of random number is always the same, unless randomize is called which initializes the random number generator with a "random" number, i.e. a tickcount.   Source Edit
proc rand(max: float): float {...}{.gcsafe, locks: 0, raises: [], tags: [].}
Returns a random number in the range 0..max. The sequence of random number is always the same, unless randomize is called which initializes the random number generator with a "random" number, i.e. a tickcount.   Source Edit
proc rand[T](r: var Rand; x: HSlice[T, T]): T
For a slice a .. b returns a value in the range a .. b.   Source Edit
proc rand[T](x: HSlice[T, T]): T
For a slice a .. b returns a value in the range a .. b.   Source Edit
proc rand[T](r: var Rand; a: openArray[T]): T {...}{.deprecated.}
Returns a random element from the openarray a. Deprecated since v0.20.0: use sample instead.   Source Edit
proc rand[T: SomeInteger](t: typedesc[T]): T
Returns a random integer in the range low(T)..high(T).   Source Edit
proc rand[T](a: openArray[T]): T {...}{.deprecated.}
returns a random element from the openarray a. Deprecated since v0.20.0: use sample instead.   Source Edit
proc sample[T](r: var Rand; a: openArray[T]): T
returns a random element from openArray a using state in r.   Source Edit
proc sample[T](a: openArray[T]): T
returns a random element from openArray a using non-thread-safe state.   Source Edit
proc sample[T, U](r: var Rand; a: openArray[T]; cdf: openArray[U]): T
Sample one element from openArray a when it has cumulative distribution function (CDF) cdf (not necessarily normalized, any type of elements convertible to float). Uses state in r. E.g.:
let val = [ "a", "b", "c", "d" ]  # some values
var cnt = [1, 2, 3, 4]            # histogram of counts
echo r.sample(val, cnt.cumsummed) # echo a sample
  Source Edit
proc sample[T, U](a: openArray[T]; cdf: openArray[U]): T
Like sample(var Rand; openArray[T], openArray[U]), but uses default non-thread-safe state.   Source Edit
proc initRand(seed: int64): Rand {...}{.raises: [], tags: [].}
Creates a new Rand state from seed.   Source Edit
proc randomize(seed: int64) {...}{.gcsafe, locks: 0, raises: [], tags: [].}
Initializes the default random number generator with a specific seed.   Source Edit
proc shuffle[T](r: var Rand; x: var openArray[T])
Swaps the positions of elements in a sequence randomly.   Source Edit
proc shuffle[T](x: var openArray[T])
Swaps the positions of elements in a sequence randomly.   Source Edit
proc randomize() {...}{.gcsafe, locks: 0, raises: [], tags: [TimeEffect].}
Initializes the random number generator with a "random" number, i.e. a tickcount. Note: Does not work for NimScript.   Source Edit