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
u8, u16, u32, u64
- to generate bounded random integers of type
u32, u64
- to generate random integers from a range,
i.e. to throw a die
- to generate random floats in interval
[0,1)
- to shuffle a slice
There are some subtilities that lead to slight biasing
or another form of non-equidistribution.[1][2]
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
Such tasks are better achieved by special crates, for example rand and ring.
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).
References