StrongRandom 2.1.0
dotnet add package StrongRandom --version 2.1.0
NuGet\Install-Package StrongRandom -Version 2.1.0
<PackageReference Include="StrongRandom" Version="2.1.0" />
<PackageVersion Include="StrongRandom" Version="2.1.0" />
<PackageReference Include="StrongRandom" />
paket add StrongRandom --version 2.1.0
#r "nuget: StrongRandom, 2.1.0"
#:package StrongRandom@2.1.0
#addin nuget:?package=StrongRandom&version=2.1.0
#tool nuget:?package=StrongRandom&version=2.1.0
StrongRandom
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)) andNextDouble()involve a transformation step that introduces a slight bias — do not rely on these for security-critical decisions such as key generation. UseNextBytes()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 a–z |
NextUppercaseLetter |
() |
Random letter A–Z |
NextLetter |
() |
Random letter a–z or A–Z |
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.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on StrongRandom:
| Package | Downloads |
|---|---|
|
Sequence
The package is intended to help generate various sequences, random and with rules. Provided is Sequence class, that given min, max and step. Calculate, without enumerating, sum, length, Variance, Standard Deviation etc.. Can enumerate lazily all elements. Also, extension methods to System.Random added. |
|
|
SimpleML.GeneticAlgorithm
Simple Genetic Algorithm implementation |
GitHub repositories
This package is not used by any popular GitHub repositories.