Skip to content

PFalkowski/StrongRandom

Repository files navigation

StrongRandom

CI Quality Gate Status Coverage NuGet version NuGet downloads License: MIT Buy Me A Coffee

System.Random drop-in backed by a Cryptographically-Secure Pseudo-Random Number Generator (CSPRNG). Everywhere you use System.Random, StrongRandom can be used instead. Also ships extension methods that add NextBool, NextByte, NextChar, NextFloat, NextNormal and more to any Random instance.

Security note: NextBytes() is a direct call to the underlying CSPRNG and is safe. Integer-returning methods (Next, Next(max), Next(min, max)) and NextDouble() involve a transformation step that introduces a slight bias — do not rely on these for security-critical decisions such as key generation. Use NextBytes() for raw entropy.

Installation

dotnet add package StrongRandom

Quick start

using Extensions.Standard.Randomization;

// Drop-in: assign to a System.Random variable
Random rng = new StrongRandom();

int dice   = rng.Next(1, 7);          // 1–6
double d   = rng.NextDouble();        // [0, 1)
byte[] key = new byte[32];
rng.NextBytes(key);                   // CSPRNG-safe raw bytes

Extension methods (Utilities)

All extensions target System.Random, so they work with StrongRandom, plain new Random(), or a mock.

Method Signature Description
NextByte (short upperLimit = 256) Random byte in [0, upperLimit)
NextBool () true or false with equal probability
NextChar (char lower = ' ', char upper = '\x7F') Random character in printable ASCII range
NextChar (string chooseFrom) Random character from a specific set
NextLowercaseLetter () Random letter az
NextUppercaseLetter () Random letter AZ
NextLetter () Random letter az or AZ
NextAlphanumeric () Random digit, uppercase, or lowercase letter
NextFloat () Random float in [0, 1)
NextFloat (float min, float max) Random float in [min, max)
NextDouble (double max) Random double in [0, max)
NextDouble (double min, double max) Random double in [min, max)
NextNormal (double mean, double sd = 1) Normally distributed value (Box-Muller)
var rng = new StrongRandom();

bool coinFlip       = rng.NextBool();
byte b              = rng.NextByte();
char letter         = rng.NextLetter();
char alphanumeric   = rng.NextAlphanumeric();
float f             = rng.NextFloat(0f, 1f);
double normal       = rng.NextNormal(mean: 0, sd: 1);
char fromSet        = rng.NextChar("AEIOU");

Customising the provider

StrongRandom depends on IRandomProvider for its byte source. The default is BufferedRandomProvider(44), which pre-fetches 44 bytes per CSPRNG call to reduce overhead.

// Larger buffer — fewer round-trips to the CSPRNG
var rng = new StrongRandom(new BufferedRandomProvider(256));

// Custom provider (e.g. for testing)
public class MyProvider : IRandomProvider
{
    public void GetBytes(byte[] input) { /* ... */ }
}
var testRng = new StrongRandom(new MyProvider());

API

StrongRandom

Inherits from System.Random. All Random members work as expected; randomness comes from the CSPRNG provider.

Member Notes
StrongRandom(IRandomProvider? provider = null) null uses BufferedRandomProvider(44)
Next() CSPRNG-backed
Next(int maxValue) CSPRNG-backed
Next(int minValue, int maxValue) CSPRNG-backed
NextDouble() CSPRNG-backed
NextBytes(byte[] buffer) Direct CSPRNG fill — unbiased

BufferedRandomProvider

Member Notes
BufferedRandomProvider(int bufferSize) Pre-fetches bufferSize bytes per CSPRNG call
GetBytes(byte[] input) Fills input from the buffer, refreshing as needed

IRandomProvider

public interface IRandomProvider
{
    void GetBytes(byte[] input);
}

License

MIT — see LICENSE.

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages