OpenTally/docs/rng.md

12 lines
876 B
Markdown
Raw Permalink Normal View History

2021-06-13 03:15:15 +10:00
# Deterministic random number generator specification
The deterministic random number generator used in OpenTally is based on an algorithm by [Ronald L Rivest](https://people.csail.mit.edu/rivest/sampler.py).
The algorithm takes a *seed* value, which is an arbitrary character string. The algorithm has, in its internal state, a *counter*, whose value is initially 0.
2022-03-11 15:44:49 +11:00
In order to generate a value between 0 (inclusive) and *n* (exclusive), to the seed is appended a comma (",") followed by the value of the counter, and a SHA-256 hash *H* is computed of the resulting string encoded using UTF-8. The hash *H* is represented as an unsigned hexadecimal integer, *k*. The counter is incremented by 1.
2021-06-13 03:15:15 +10:00
In order to avoid modulo bias, if *k* ≥ ⌊*M*/*n*⌋ × *n* (where *M* = 2^256), *k* is discarded and the algorithm is repeated.
Otherwise, the result is *k* modulo *n*.