StartQuick start
Quick start
Install the packages, register the generator and three beacons, make a secret, draw values and run a draw anyone can check.
This page takes you from an empty project to a registered generator and three beacons, a secret, drawn values and a draw that anyone can check.
Install
Section titled “Install”dotnet add package NCode.Randomnessdotnet add package NCode.Randomness.Curbydotnet add package NCode.Randomness.Nistdotnet add package NCode.Randomness.DrandNCode.Randomness is the generator and the draw API. Each other package adds one beacon, and you need one only for a draw that others check.
Register the generator
Section titled “Register the generator”AddRandomness registers ISecureRandom, and each Add…Beacon call adds a beacon. Building the host contacts no beacon. Once the host runs, a background collector asks every beacon for its latest pulse, at start and again every minute in the default mode, and the generator mixes in what they deliver; a draw fetches the round it names when you make it. The Offline mode keeps the collector from contacting any beacon, as choose a mode shows. To go further, see how to add beacons.
var builder = Host.CreateApplicationBuilder();builder .Services.AddRandomness() .AddCurbyBeacon() .AddNistBeacon() .AddDrandBeacon();builder.Services.AddHealthChecks().AddRandomnessBeacons("ready");
using var host = builder.Build();var random = host.Services.GetRequiredService<ISecureRandom>();Make a secret
Section titled “Make a secret”Anything that must stay unguessable comes from ISecureRandom, which draws from the operating system and never from a beacon. To go further, see how to make a secret.
public sealed class TokenService(ISecureRandom random){ private const string Alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
public string NewRecoveryCode() => random.GetString(Alphabet, 16);
public int RollDie() => random.NextInteger(1, 7);}Draw values
Section titled “Draw values”ISecureRandom also gives unbiased integers in a range, a uniform double, a Guid, a shuffle and a pick from a list.
var roll = random.NextInteger(1, 7); // int in [1, 7)long offset = random.NextInteger(-1_000L, 1_000L); // long in [-1000, 1000)UInt128 id = random.NextInteger<UInt128>(); // all 128 bitsdouble ratio = random.NextDouble(); // [0, 1), 53 random bitsGuid token = random.NextGuid(); // RFC 9562 version 4random.Shuffle<int>(deck); // Fisher–Yatesvar color = random.GetItem<string>(["red", "green", "blue"]);Random legacy = random.AsRandom(); // for APIs that take a System.RandomRun a draw anyone can check
Section titled “Run a draw anyone can check”A draw takes its randomness from one beacon round and a context you choose, so anyone who has the round and the context gets the same values. To go further, see how to run a public draw.
using var draw = await draws.CreateAsync( "curby", round: 1620423, context: "example-raffle-2026-11-25", cancellationToken);var winner = entrants[draw.NextInteger(0, entrants.Length)];// Publish draw.Pulse.Id with the result, so anyone can recompute it.Next steps
Section titled “Next steps”- Which API? helps you pick the API that fits the value you need.
- Make a secret shows how to make a key, a token or a code.
- Add beacons shows how to register a beacon and configure it.
- Run a public draw shows how to commit to a round and publish a result that others can recompute.