Lyricdev
GitHub

std.random

Random numbers: Random, a ChaCha8 generator, and free functions over the running thread's own generator.

A generator is either seeded — its stream stays the same through every 5.x — or fresh from the system. None of this is for secrets: those come from std.crypto's randomBytes.

The seeded stream: the key comes from the seed through SplitMix64 — four outputs, eight words, the low half of each first —, the nonce is zero, a 64-bit block counter runs from zero in words 12 and 13, and each block's sixteen words are used in order, two of them a uint64, the first the low half.

Examples

import std.io { println };
import std.random { Random };

let a = Random.seeded(7);
let b = Random.seeded(7);
let x = a.nextInRange(1..7);
println(x == b.nextInRange(1..7));
println(x >= 1 && x < 7);
true
true

Types

Random class — A generator of random numbers: ChaCha8 (eight rounds), 64 bits a draw.

Functions

pub fn nextInRange(r: Range<int>): int

Returns an integer of r, each alike, from the thread's generator.

Panics

Panics if r is empty.

pub fn nextFloat(): float

Returns a float in [0, 1) from the thread's generator.

pub fn shuffle<T>(s: Slice<T>): void

Shuffles s in place with the thread's generator.