Publish release notes for v0.2
fd2adc1b80c9 — Michael Johnson v0.2 2 months ago
Update README.md with new site url
befbd479ce05 — Michael Johnson 2 months ago
Add release notes into NuGet package and update project url

heads

tip
browse log

clone

read-only
https://hg.sr.ht/~ociaw/RandN
read/write
ssh://hg@hg.sr.ht/~ociaw/RandN

#RandN

RandN on NuGet

RandN is a .NET library for random number generation. It aims to rectify deficiencies in System.Random with adaptability and extensibility in mind. RandN is heavily inspired by the design of the Rust crate rand, and aims to maintain some level of compatibility with it.

#What's wrong with System.Random?

In short, the algorithm it uses is slow and biased. The API is very rigid and inflexible, and as a result is unsuited for many purposes.

RandN provides a clear and obvious API that is difficult to use incorrectly, unlike the API of System.Random. This is accomplished by clearly separating two concepts; generating randomness with an IRng, and turning that data into something useful with an IDistribution.

#Docs

The full documentation is available here.

#Usage

Install the RandN package from NuGet for most use cases. If you just want to implement an random number generator (ex. you're publishing a package with a new RNG), instead depend on RandN.Core.

#Examples

#Creating an RNG

using RandN;

// Creates a cryptographically secure RNG
var rng = StandardRng.Create();

// Creates a non-cryptographically secure RNG that's fast and uses less memory
var insecureRng = SmallRng.Create();

A reproducible RNG can also be created by using an algorithm directly:

using RandN.Rngs;

// Use ThreadLocalRng to seed the RNG - this uses a cryptographically secure
// algorithm, so tight loops won't result in similar seeds
var seeder = ThreadLocalRng.Instance;

// Create the seed (Seeds can also be created manually)
var factory = ChaCha.GetChaCha8Factory();
var seed = factory.CreateSeed(seeder);

// Create the RNG from the seed
var rng = factory.Create(seed);

#Getting random numbers

Once you have an RNG, you can either use it directly,

var num = rng.NextUInt32();
var bigNum = rng.NextUInt64();
var buffer = new Byte[1000];
rng.Fill(buffer);

or you can use it to sample a distribution:

Uniform.Int32 distribution = Uniform.NewInclusive(42, 54); // [42 - 54]
int answer = distribution.Sample(rng);

Bernoulli weightedCoin = Bernoulli.FromRatio(8, 10); // 80% chance of true
bool probablyHeads = weightedCoin.Sample(rng);

Shuffling a list is also easy:

var list = new List<Int32>() { 1, 2, 3, 4, 5, 6 };
rng.ShuffleInPlace(list);

Any type implementing IRng can be wrapped with RandomShim, which can be used as a drop-in replacement for Random.

using RandN.Compat;
Random random = RandomShim.Create(rng);
random.Next(2); // returns 0 or 1