![]() |
VOOZH | about |
dotnet add package Tedd.RandomUtils --version 1.0.1
NuGet\Install-Package Tedd.RandomUtils -Version 1.0.1
<PackageReference Include="Tedd.RandomUtils" Version="1.0.1" />
<PackageVersion Include="Tedd.RandomUtils" Version="1.0.1" />Directory.Packages.props
<PackageReference Include="Tedd.RandomUtils" />Project file
paket add Tedd.RandomUtils --version 1.0.1
#r "nuget: Tedd.RandomUtils, 1.0.1"
#:package Tedd.RandomUtils@1.0.1
#addin nuget:?package=Tedd.RandomUtils&version=1.0.1Install as a Cake Addin
#tool nuget:?package=Tedd.RandomUtils&version=1.0.1Install as a Cake Tool
Extension methods for System.Random that adds support for more datatypes. NextBoolean, NextSByte(), NextByte(), NextInt16(), NextUInt16(), NextIn32(), NextUInt32(), NextInt64(), NextUInt64(), NextFloat() and NextString().
ConcurrentRandom provides a lock free thread safe static way to access System.Random. This is implemented by creating a new System.Random object per thread (ThreadLocal), seeded by a root random object protected by a spinlock.
CryptoRandom uses the operating systems underlying CSP (Cryptographic Service Provider) for better random data. See further down for explanation.
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 without locking.
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);
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);
Drop-in replacement for System.Random that gets more random data from Cryptographic Service Provider.
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
}
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
System.Random is based on a 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. CryptoRandom mimics System.Random and is a drop-in replacement. You get the power of RandomNumberGenerator with the ease of System.Random.
Created using .Net Standard 1.3.
xUnit in .Net Core with near 100% code coverage. Boundary checks as well as average check (for statistical distribution) on vast number of samples.
Standard System.Random.Next() returns a positive integer, while all this library return full range of values for given datatype. Standard System.Random.Next(from, to) has "exclusive to" value, meaning it only returns 31 random bits in the 32-bit integer. This library returns random for all 32 and 64 bits on NextInt32(), NextUInt32, NextInt64() and NextUInt64() respectively.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. 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 is compatible. netcoreapp2.1 netcoreapp2.1 is compatible. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 is compatible. netcoreapp3.1 netcoreapp3.1 is compatible. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 is compatible. net462 net462 was computed. 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 | 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. |
Showing the top 1 NuGet packages that depend on Tedd.RandomUtils:
| Package | Downloads |
|---|---|
|
GameFrameX.Utility
GameFrameX.Utility,GameFrameX 框架的基础设施框架库.框架文档主页: https://gameframex.doc.alianblank.com |
This package is not used by any popular GitHub repositories.