Skip to content

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.

Terminal window
dotnet add package NCode.Randomness
dotnet add package NCode.Randomness.Curby
dotnet add package NCode.Randomness.Nist
dotnet add package NCode.Randomness.Drand

NCode.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.

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>();

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);
}

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 bits
double ratio = random.NextDouble(); // [0, 1), 53 random bits
Guid token = random.NextGuid(); // RFC 9562 version 4
random.Shuffle<int>(deck); // Fisher–Yates
var color = random.GetItem<string>(["red", "green", "blue"]);
Random legacy = random.AsRandom(); // for APIs that take a System.Random

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.
  • 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.