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
wwords, 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, asrolldraws it, from the words ofrng, which is an item’s (Item) where the draw is one of a call’s. - deal
- Appends
ndistinct integers belowtotaltoout, in random order. Every choice and order is equally likely.nmust be at mosttotal. - roll
- Appends
nintegers toout, 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 ofitems, which packs one word for each item, and is the number thatrand’sUniformgives for its bound from the item’s words, by Lemire’s method. A draw maps a wordwto the high word ofw × b, unless the low word is below 2⁶⁴ mod b. Then it discardswand 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’sOpen01makes from the wordw: 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 whatOpen01gives. - units
- Appends
nfloats toout, eachunitof its item’s word: the numbers that drawingOpen01once from each of the nextnitems ofitemsgives.itemspacks one word for each item. Reading every word before converting any lets the conversion run as a separate, vectorized pass.