Giter Site home page Giter Site logo

randn's Introduction

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

randn's People

Contributors

ociaw avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.