Often, during development and implementation of algorithms, one is in need of a basic random number generator. For example, a sorting algorithm may be tested against shuffled arrays.
For convenience and to increase productivity, I think it is a good idea to have a basic random number generator available.
What functionality should it have?
In my opinion an RNG API should at least be able to achieve the following:
- to generate random integers of type
- to generate bounded random integers of type
- to generate random integers from a range, i.e. to throw a die
- to generate random floats in interval
- to shuffle a slice
There are some subtilities that lead to slight biasing or another form of non-equidistribution. Under this premise, generating bounded integers and random floats are better not achieved by a self-built procedure.
In addition, it should be easy and elegant:
- to randomly pick an element from a slice
- to fill a buffer with random numbers
- to efficiently fill a buffer with random bytes
- to create an iterator, producing random numbers
The module should not:
- have a generator with state space larger than 256 bit
- have any fancy stuff, i.e. ways to produce samples of custom distributions
- have a cryptographically secure generator
libstd or libcore?
The floating point RNG might call a function from “libmath”. Preventing a potential dependency hell, it might be better to leave everything in libstd first.
A schedule proposition
- We open a challenge. People make proposals which will be criticized.
- After some time the most appropriate design will be chosen.
- We hold it unstable two years or longer, in order everybody can have an eye upon it.
A first sketch
To start the challenge, this is my first sketch:
The API is more important than the internal implementation. But note that the internals can have an effect on the API too. For example, if a generator has 128 bit state space or more than one state space, it might also be seeded by
(seed0: u64, seed1: u64).
-  M.E. O’Neill: “Efficiently Generating a Number in a Range”.
-  Taylor R Campbell: “Uniform random floats: How to generate a double-precision floating-point number in [0, 1] uniformly at random given a uniform random source of bits”.