idontgetoutmuch / random

Random number library
Other
3 stars 2 forks source link

Prior art: API design #13

Open curiousleo opened 4 years ago

curiousleo commented 4 years ago

Prior art in "standard" libraries

Go

Golang has two APIs, one for non-crypto PRNGs and one for cryptographically random bytes:

Rust

Rust has a family of packages for PRNGs. The basic interfaces are defined in rand_core.

C++

The C++ standard defines a set of highly templated classes which together form the PRNG API.

O'Neill makes the point that this API is flexible, but hard to use correctly, and proposes an alternative interface. IMO it's a nice API that provides safe defaults but is still very flexible for those who want flexibility.

numpy.random

Inspired by O'Neill.

Prior art in Haskell libraries

splitmix

Pure, splittable SplitMix implementation.

mwc-random

Provides a monadic generator interface for Marsaglia's MWC256 (a.k.a. MWC8222) multiply-with-carry generator, a way to access system randomness, and a way to draw from a number of random distributions. Does not provide a way to split/spawn generators or seeds.

random-source

Defines the MonadRandom typeclass, an interface for random number generation. One aim of our effort here should be to make random-source unnecessary (see https://github.com/idontgetoutmuch/random/issues/4#issuecomment-592950462).

random-fu

Provides a way to draw from a number of random distributions. The randomness source is pluggable AFAICT.

tf-random

Implements a splittable PRNG implementing the old random's RandomGen, provides alternative interfaces, and a way to access system randomness. Based on ThreeFish.

More prior art

Common concepts

The API split between randomness sources and random distributions is apparent in every API I looked at:

Note that drawing from a distribution is what random-fu exists for in the Haskell world. I believe it is worth understanding if and what changes would be required to make random-fu work with the new random. Apart from that, it seems to cover the issue of drawing from a distribution well, so we can concentrate on building a solid API for randomness sources and seeding here.

Seeding

Seeding is handled a bit differently from API to API.

idontgetoutmuch commented 4 years ago

An interesting discussion https://github.com/JuliaLang/julia/issues/27614 not too dissimilar to the one we've been having.