VOOZH about

URL: https://www.nuget.org/packages/Tedd.RandomUtils/

⇱ NuGet Gallery | Tedd.RandomUtils 1.0.6




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.
<PackageVersion Include="Tedd.RandomUtils" Version="1.0.6" />
 
Directory.Packages.props
<PackageReference Include="Tedd.RandomUtils" />
 
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 Tedd.RandomUtils --version 1.0.6
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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.
#:package Tedd.RandomUtils@1.0.6
 
 
#: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=Tedd.RandomUtils&version=1.0.6
 
Install as a Cake Addin
#tool nuget:?package=Tedd.RandomUtils&version=1.0.6
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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 Versions Compatible and additional computed target framework versions.
.NET net5.0 net5.0 is compatible.  net5.0-windows net5.0-windows was computed.  net6.0 net6.0 was computed.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 was computed.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
.NET Core netcoreapp2.0 netcoreapp2.0 was computed.  netcoreapp2.1 netcoreapp2.1 was computed.  netcoreapp2.2 netcoreapp2.2 was computed.  netcoreapp3.0 netcoreapp3.0 was computed.  netcoreapp3.1 netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 netstandard2.0 is compatible.  netstandard2.1 netstandard2.1 is compatible. 
.NET Framework net461 net461 was computed.  net462 net462 is compatible.  net463 net463 was computed.  net47 net47 was computed.  net471 net471 was computed.  net472 net472 was computed.  net48 net48 was computed.  net481 net481 was computed. 
MonoAndroid monoandroid monoandroid was computed. 
MonoMac monomac monomac was computed. 
MonoTouch monotouch monotouch was computed. 
Tizen tizen40 tizen40 was computed.  tizen60 tizen60 was computed. 
Xamarin.iOS xamarinios xamarinios was computed. 
Xamarin.Mac xamarinmac xamarinmac was computed. 
Xamarin.TVOS xamarintvos xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Tedd.RandomUtils:

Package Downloads
GameFrameX.Utility

GameFrameX.Utility,GameFrameX 框架的基础设施框架库.框架文档主页: https://gameframex.doc.alianblank.com

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.6 38,330 1/26/2021
1.0.5 846 12/18/2020
1.0.4 1,371 5/1/2020
1.0.3 980 3/25/2020
1.0.2 763 2/1/2020
1.0.1 770 12/21/2019
1.0.0 746 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