Tedd.RandomUtils 1.0.6

dotnet add package Tedd.RandomUtils --version 1.0.6                
NuGet\Install-Package Tedd.RandomUtils -Version 1.0.6                
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="Tedd.RandomUtils" Version="1.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tedd.RandomUtils --version 1.0.6                
#r "nuget: Tedd.RandomUtils, 1.0.6"                
#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.
// Install Tedd.RandomUtils as a Cake Addin
#addin nuget:?package=Tedd.RandomUtils&version=1.0.6

// Install Tedd.RandomUtils as a Cake Tool
#tool nuget:?package=Tedd.RandomUtils&version=1.0.6                

Random by type

var rnd = new Random();

bool   val1  = rnd.NextBoolean();
sbyte  val2  = rnd.NextSByte();
byte   val3  = rnd.NextByte();
short  val4  = rnd.NextInt16();
ushort val5  = rnd.NextUInt16();
int    val6  = rnd.NextInt32();
uint   val7  = rnd.NextUInt32();
long   val8  = rnd.NextInt64();
ulong  val9  = rnd.NextUInt64();
float  val10 = rnd.NextFloat();
string val11 = rnd.NextString("abcdefg", 8);

Thread safe random

Thread safe random.

bool   val1  = ConcurrentRandom.NextBoolean();
sbyte  val2  = ConcurrentRandom.NextSByte();
byte   val3  = ConcurrentRandom.NextByte();
short  val4  = ConcurrentRandom.NextInt16();
ushort val5  = ConcurrentRandom.NextUInt16();
int    val6  = ConcurrentRandom.NextInt32();
uint   val7  = ConcurrentRandom.NextUInt32();
long   val8  = ConcurrentRandom.NextInt64();
ulong  val9  = ConcurrentRandom.NextUInt64();
float  val10 = ConcurrentRandom.NextFloat();
string val11 = ConcurrentRandom.NextString("abcdefg", 8);
ConcurrentRandom.NextBytes(byteArray);

Crypto strength random

using rnd = new CryptoRandom();

bool   val1  = rnd.NextBoolean();
sbyte  val2  = rnd.NextSByte();
byte   val3  = rnd.NextByte();
short  val4  = rnd.NextInt16();
ushort val5  = rnd.NextUInt16();
int    val6  = rnd.NextInt32();
uint   val7  = rnd.NextUInt32();
long   val8  = rnd.NextInt64();
ulong  val9  = rnd.NextUInt64();
float  val10 = rnd.NextFloat();
string val11 = rnd.NextString("abcdefg", 8);
rnd.NextBytes(byteArray);

Thread safe random

Thread safe crypto strength random.

bool   val1  = ConcurrentCryptoRandom.NextBoolean();
sbyte  val2  = ConcurrentCryptoRandom.NextSByte();
byte   val3  = ConcurrentCryptoRandom.NextByte();
short  val4  = ConcurrentCryptoRandom.NextInt16();
ushort val5  = ConcurrentCryptoRandom.NextUInt16();
int    val6  = ConcurrentCryptoRandom.NextInt32();
uint   val7  = ConcurrentCryptoRandom.NextUInt32();
long   val8  = ConcurrentCryptoRandom.NextInt64();
ulong  val9  = ConcurrentCryptoRandom.NextUInt64();
float  val10 = ConcurrentCryptoRandom.NextFloat();
string val11 = ConcurrentCryptoRandom.NextString("abcdefg", 8);
ConcurrentCryptoRandom.NextBytes(byteArray);

Fast random

using rnd = new FastRandom();

bool   val1  = rnd.NextBoolean();
sbyte  val2  = rnd.NextSByte();
byte   val3  = rnd.NextByte();
short  val4  = rnd.NextInt16();
ushort val5  = rnd.NextUInt16();
int    val6  = rnd.NextInt32();
uint   val7  = rnd.NextUInt32();
long   val8  = rnd.NextInt64();
ulong  val9  = rnd.NextUInt64();
float  val10 = rnd.NextFloat();
string val11 = rnd.NextString("abcdefg", 8);
rnd.NextBytes(byteArray);

CryptoRandom

Drop-in replacement for System.Random that gets more random data from Cryptographic Service Provider.

Example

Works exactly like System.Random, except you may want to dispose of it when you are done. (If you don't dispose of it, the destructor will do it for you upon garbage collect.)

var rnd = new CryptoRandom();
var dice = rnd.Next(1, 7); // A random number between 1 and 6 inclusive
rnd.Dispose();

Or with using:

using (var rnd = new CryptoRandom()) {
	var percent = rnd.NextDouble() * 100;
	Console.WriteLine($"You are {percent}% done, please wait...");
}

Note that it is recommended to create a shared Random object, and in case of multiple threads use synchronized access to generate random data.

public static class Main {
	public static CryptoRandom Rnd = new CryptoRandomRandom();

	public static void Start() {
		int dice;
		lock (Rnd)
			dice = Rnd.Next(1, 7); // A random number between 1 and 6 inclusive
	}
}

Benchmarks

Each execution is 1.000.000 calculation of random + loop overhead. So 2.381 ms / 1M is 0.002381 ms per calculation = 2.381 us (microseconds). In case of FastRandom the loop overhead may account for around 50% of the time.


BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
AMD Ryzen 9 3950X, 1 CPU, 32 logical and 16 physical cores
.NET Core SDK=3.1.101
  [Host]                : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
  x64 .Net Core 3.1 Ryu : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT

Job=x64 .Net Core 3.1 Ryu  Jit=RyuJit  Platform=X64  
Runtime=.NET Core 3.1  Force=True  InvocationCount=1  
IterationCount=100  LaunchCount=1  UnrollFactor=1  
WarmupCount=15  

Method Mean Error StdDev Median Min Max P95 P90 Iterations Op/s Ratio RatioSD Baseline Gen 0 Gen 1 Gen 2 Allocated TotalIssues/Op BranchInstructions/Op BranchMispredictions/Op
FastRandom 2.381 ms 0.0019 ms 0.0055 ms 2.380 ms 2.372 ms 2.394 ms 2.392 ms 2.387 ms 95.00 420.08 0.32 0.00 No - - - - 4,081,116 1,053,191 31,383
SystemRandom 7.331 ms 0.0048 ms 0.0125 ms 7.330 ms 7.312 ms 7.373 ms 7.350 ms 7.348 ms 78.00 136.41 1.00 0.00 Yes - - - - 17,775,692 5,953,001 353,063
CryptoRandom 7.486 ms 0.0042 ms 0.0108 ms 7.485 ms 7.467 ms 7.526 ms 7.506 ms 7.499 ms 77.00 133.58 1.02 0.00 No - - - - 17,813,788 5,968,906 354,385
ConcurrentRandom 30.236 ms 0.0179 ms 0.0529 ms 30.218 ms 30.161 ms 30.390 ms 30.355 ms 30.303 ms 100.00 33.07 4.12 0.01 No - - - - 66,216,751 22,077,440 1,261,947
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 6,914 1/26/2021
1.0.5 574 12/18/2020
1.0.4 1,088 5/1/2020
1.0.3 716 3/25/2020
1.0.2 477 2/1/2020
1.0.1 509 12/21/2019
1.0.0 504 12/19/2019

Added ConcurrentCryptoRandom.
Added FastRandom based on Lehmer which is 70% faster than System.Random.
Increased speed of ConcurrentRandom by 25% by avoiding ThreadLocal.
Increased speed of