Skip to content

Local Drawing & Data

SecRandom.DrawKit 1.2 manages local rosters and history and performs random or fair draws without installing or running SecRandom. The SQLite database belongs to the integrating application and does not automatically read or modify SecRandom data.

Install SecRandom.DrawKit and keep SecRandom.Integration.Abstractions in the local package source. Its Microsoft.Data.Sqlite dependency requires nuget.org or a configured mirror. Packages target .NET 8 and compatible runtimes.

using SecRandom.DrawKit;
using SecRandom.Integration;
var store = new LocalDrawStore("data/classroom.sqlite");
store.SaveRoster(new Roster("class-a", "Class A", RosterKind.Students,
[new RosterMember("student-1", "Alice"), new RosterMember("student-2", "Bob")]));
store.SaveMember("class-a", new RosterMember("student-3", "Casey", Group: "Group 1"));
var result = store.Draw(new DrawRequest("class-a", 1, DrawMode.Fair)
{ FairStrategy = FairDrawStrategy.RandomFirst });
Console.WriteLine(result.Members[0].Name);
var page = store.QueryHistory(new HistoryQuery(RosterId: "class-a", Limit: 50));
var summary = store.GetHistorySummary("class-a");

Roster and member IDs must be stable and nonempty; member IDs must be unique within a roster. SaveRoster creates or replaces a roster; FindRoster and ListRosters query it. SaveMember adds or updates a member; DeleteMember deletes one. DeleteRoster retains history.

DrawMode.Random samples eligible enabled members uniformly without replacement. DrawMode.Fair uses each member’s history count and weight. FairDrawStrategy.UniformFirst (default) narrows the pool to lower counts and relaxes it when necessary; RandomFirst keeps the pool and adjusts weights based on the count gap, relaxing the threshold if necessary. Zero-weight members cannot be selected in fair mode. Both modes allow at most 200 selections per draw. These are SDK rules and do not implement every SecRandom fairness setting, orchestration, or draw proof.

Draw reads the roster and history, chooses members, and records the result in one SQLite transaction. SQLite serializes concurrent writers. Counts use stable member IDs, so renaming a member keeps their history; deleting history changes future fair weights.

QueryHistory supports roster and time filters, pagination, and a total count. GetHistorySummary returns rounds, selections, and per-member counts. AddHistory, DeleteHistory, and ClearHistory manage records. Deletions are not automatically reversible.

Export returns a LocalDrawSnapshot containing all rosters and history for serialization or backup. Import is atomic and rejects duplicate roster or round IDs. The caller owns file permissions, transfer, and backup location. The legacy JSON stores remain for compatibility; new applications should use LocalDrawStore.