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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Utils.Random.Security" Version="4.0.307" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Utils.Random.Security" Version="4.0.307" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Utils.Random.Security" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Utils.Random.Security --version 4.0.307
                    
#r "nuget: Soenneker.Utils.Random.Security, 4.0.307"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Utils.Random.Security@4.0.307
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Utils.Random.Security&version=4.0.307
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Utils.Random.Security&version=4.0.307
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

alternate text is missing from this package README image 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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 182 8/31/2026
4.0.306 109 8/30/2026
4.0.305 207 8/29/2026
4.0.304 86 8/29/2026
4.0.303 335 7/27/2026
4.0.302 206 7/16/2026
4.0.301 1,432 6/18/2026
4.0.298 521 4/23/2026
4.0.296 472 3/12/2026
4.0.293 281 3/10/2026
4.0.292 220 3/9/2026
4.0.291 139 3/9/2026
4.0.290 233 3/4/2026
4.0.289 399 1/18/2026
4.0.288 299 1/2/2026
4.0.287 627 11/20/2025
4.0.286 307 11/6/2025
4.0.285 322 10/29/2025
3.0.284 454 9/3/2025
3.0.283 329 8/11/2025
Loading failed

Clarify secure random generation