Tedd.MoreRandom
1.0.1
Further updates to this package is done through RandomUtils.
Install-Package Tedd.MoreRandom -Version 1.0.1
dotnet add package Tedd.MoreRandom --version 1.0.1
<PackageReference Include="Tedd.MoreRandom" Version="1.0.1" />
paket add Tedd.MoreRandom --version 1.0.1
#r "nuget: Tedd.MoreRandom, 1.0.1"
// Install Tedd.MoreRandom as a Cake Addin
#addin nuget:?package=Tedd.MoreRandom&version=1.0.1
// Install Tedd.MoreRandom as a Cake Tool
#tool nuget:?package=Tedd.MoreRandom&version=1.0.1
Tedd.MoreRandom
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 Tedd.MoreRandom.Random();
var dice = rnd.Next(1, 7); // A random number between 1 and 6 inclusive
rnd.Dispose();
Or with using:
using (var rnd = new Tedd.MoreRandom.Random()) {
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 Tedd.MoreRandom.Random Rnd = new Tedd.MoreRandom.Random();
public static void Start() {
int dice;
lock (Rnd)
dice = Rnd.Next(1, 7); // A random number between 1 and 6 inclusive
}
}
Thread safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Background
System.Random is based on so called "pseudorandom" algorithm. This means that given a seed (default: number of milliseconds since computer was started) math is used to generate seemingly random numbers. If given the same seed, a sequence of random numbers will look the same every time. For most cases this is fine, but in some cases you need more random data. One such case is cryptography, where a pseudorandom generator such as System.Random would generate a predictable sequence of numbers.
RNGCryptoServiceProvider through RandomNumberGenerator provides "cryptography grade random" numbers. These numbers are a bit more random as they are provided by the operating system, which has methods of collecting random data.
RandomNumberGenerator gives you a bunch of random bytes. It's up to you to convert to a number and size for whatever purpose. System.Random however has a simple interface, for example rnd.Next(10).
This is where MoreRandom comes in. Tedd.MoreRandom.Random mimics System.Random and is a drop-in replacement. You get the power of RandomNumberGenerator with the ease of System.Random.
Compatibility
Created using .Net Standard 1.3.
Unit testing
xUnit in .Net Core with near 100% code coverage. Boundary checks as well as average check (for statistical distribution) on vast number of samples.
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows |
.NET Core | netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1 |
.NET Framework | net46 net461 net462 net463 net47 net471 net472 net48 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen30 tizen40 tizen60 |
Universal Windows Platform | uap uap10.0 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 1.3
- NETStandard.Library (>= 1.6.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added GetByteArray