Soenneker.Utils.Random.Security
4.0.307
Prefix Reserved
dotnet add package Soenneker.Utils.Random.Security --version 4.0.307
NuGet\Install-Package Soenneker.Utils.Random.Security -Version 4.0.307
<PackageReference Include="Soenneker.Utils.Random.Security" Version="4.0.307" />
<PackageVersion Include="Soenneker.Utils.Random.Security" Version="4.0.307" />
<PackageReference Include="Soenneker.Utils.Random.Security" />
paket add Soenneker.Utils.Random.Security --version 4.0.307
#r "nuget: Soenneker.Utils.Random.Security, 4.0.307"
#:package Soenneker.Utils.Random.Security@4.0.307
#addin nuget:?package=Soenneker.Utils.Random.Security&version=4.0.307
#tool nuget:?package=Soenneker.Utils.Random.Security&version=4.0.307
Soenneker.Utils.Random.Security
Generates cryptographically secure random bytes, bounded integers, decimals, and unit-interval doubles with RandomNumberGenerator.
Installation
dotnet add package Soenneker.Utils.Random.Security
Quick start
using Soenneker.Utils.Random.Security;
Call the static RandomSecurityUtil methods directly; no dependency-injection registration is required.
The methods use RandomNumberGenerator and are appropriate when unpredictability matters. They do
not manage storage, encoding, expiration, comparison, or secret rotation for you.
Secret bytes
using System.Security.Cryptography;
byte[] secret = RandomSecurityUtil.GetByteArray(32); // 256 random bits
try
{
UseSecret(secret);
}
finally
{
CryptographicOperations.ZeroMemory(secret);
}
GetByteArray(length) allocates and fills a new array. A zero length returns Array.Empty<byte>().
The caller owns the result and should clear it when practical. If you already own a buffer, call
the BCL's RandomNumberGenerator.Fill(span) directly to avoid another allocation.
Encode random bytes with an encoding designed for the destination when a textual token is needed; do not decode arbitrary random bytes as UTF-8. Remember that the resulting managed string cannot be securely erased.
Bounded values
int index = RandomSecurityUtil.Next(0, choices.Count); // [0, Count)
decimal amount = RandomSecurityUtil.NextDecimal(10m, 20m); // [10, 20)
Next delegates to the unbiased RandomNumberGenerator.GetInt32 implementation and requires an
exclusive upper bound greater than the lower bound.
NextDecimal scales a 28-digit random fraction and rejects any result rounded to the exclusive
upper endpoint. Decimal range subtraction can still throw OverflowException when the requested
interval spans beyond decimal's representable arithmetic range. Use integer sampling when every
possible outcome must have a directly auditable discrete probability.
Unit fractions
decimal fraction = RandomSecurityUtil.GetRandomFraction(); // [0, 1), 28 decimal places
double sample = RandomSecurityUtil.NextDouble(); // [0, 1), 53 random bits
GetRandomFraction uses rejection sampling across the decimal coefficient range, without
floating-point conversion. NextDouble samples uniformly from values of the form k / 2^53.
Although their entropy source is cryptographic, floating-point and decimal samples are usually
better suited to randomized application behavior than to constructing keys or tokens; use random
bytes for those.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Soenneker.Utils.Random.Security:
| Package | Downloads |
|---|---|
|
Soenneker.Hashing.Argon2
A utility library for Argon2 hashing and verification |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.307 | 168 | 8/31/2026 |
| 4.0.306 | 108 | 8/30/2026 |
| 4.0.305 | 205 | 8/29/2026 |
| 4.0.304 | 85 | 8/29/2026 |
| 4.0.303 | 334 | 7/27/2026 |
| 4.0.302 | 205 | 7/16/2026 |
| 4.0.301 | 1,431 | 6/18/2026 |
| 4.0.298 | 518 | 4/23/2026 |
| 4.0.296 | 472 | 3/12/2026 |
| 4.0.293 | 280 | 3/10/2026 |
| 4.0.292 | 219 | 3/9/2026 |
| 4.0.291 | 135 | 3/9/2026 |
| 4.0.290 | 232 | 3/4/2026 |
| 4.0.289 | 398 | 1/18/2026 |
| 4.0.288 | 299 | 1/2/2026 |
| 4.0.287 | 626 | 11/20/2025 |
| 4.0.286 | 306 | 11/6/2025 |
| 4.0.285 | 321 | 10/29/2025 |
| 3.0.284 | 453 | 9/3/2025 |
| 3.0.283 | 328 | 8/11/2025 |
Clarify secure random generation