Skip to main content

Module random

Module random 

Source
Expand description

Random sampling from counter streams. Each call reads one ChaCha8 stream selected by a key and stream number. Item k reads its first w words at position k*w, and rejection sampling uses that item’s separate region. Floats, rolls and distribution samplers share this word addressing.

Structs§

Counter
A generator’s state: its key and the count of its draw calls. Draw call c reads stream c under the key, from its start, so its numbers don’t depend on how many numbers the calls before it took.
Item
One item’s words: its packed ones, then its region’s (Items). Dropping it skips the packed words it didn’t read, so that the next item reads its own.
Items
The words of one draw call’s items, item by item. Item k reads its first w words, the words of one attempt of the call’s law, at word k × w of the call’s stream. It reads any further words from its own region, from block 2⁶³ + k × 2²³ on.
Key
A ChaCha8 key. Every stream that one generator’s draw calls read comes from it.
Stream
The stream that one draw call reads: a key and a stream number under it.

Functions§

below
One integer from 0 up to but not including b, as roll draws it, from the words of rng, which is an item’s (Item) where the draw is one of a call’s.
deal
Appends n distinct integers below total to out, in random order. Every choice and order is equally likely. n must be at most total.
roll
Appends n integers to out, each uniform from 0 up to but not including its bound: the one bound, or each of the bounds in turn. Each bound must be at least 1. Each draw reads the next item of items, which packs one word for each item, and is the number that rand’s Uniform gives for its bound from the item’s words, by Lemire’s method. A draw maps a word w to the high word of w × b, unless the low word is below 2⁶⁴ mod b. Then it discards w and maps the item’s next word, from its region, instead. Reading every packed word before mapping any lets the mapping run as a separate pass.
unit
The float in the open interval (0, 1) that rand’s Open01 makes from the word w: its top 52 bits as the fraction of a number in [1, 2), less 1 − ε/2. Seeded draws depend on this staying bit for bit what Open01 gives.
units
Appends n floats to out, each unit of its item’s word: the numbers that drawing Open01 once from each of the next n items of items gives. items packs one word for each item. Reading every word before converting any lets the conversion run as a separate, vectorized pass.