Class RandomGenerator
Represents a random generator which is based on System.Random() yet has an ability to feed external samples into it.
Use RandomGenerator.Instance to use the default thread-safe instance or use App.Random shortcut
Inheritance
System.Object
RandomGenerator
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Assembly: Azos.dll
public sealed class RandomGenerator
Constructors
Create new instance of ExternalRandomGenerator. Create new instances only if you need to use different sample ring buffers.
In majority of cases use ExternalRandomGenerator.Instance to use default instance instead of creating a new instance.
Default instance is thread-safe for process-wide use
Declaration
Properties
Returns the default process-wide instance of the generator. This instance is thread-safe
Declaration
public static RandomGenerator Instance { get; }
Property Value
Generates random byte[16] buffer
Declaration
public byte[] NextRandom16Bytes { get; }
Property Value
| Type |
Description |
| System.Byte[] |
|
Returns 0..1 random double
Declaration
public double NextRandomDouble { get; }
Property Value
| Type |
Description |
| System.Double |
|
Generates next random integer in the Int32.MinValue..Int32.MaxValue range
Declaration
public int NextRandomInteger { get; }
Property Value
| Type |
Description |
| System.Int32 |
|
Generates next random integer in the Uint32.MinValue..Uint32.MaxValue diapason
Declaration
public uint NextRandomUnsignedInteger { get; }
Property Value
| Type |
Description |
| System.UInt32 |
|
Generates next random ulong in the Uint64.MinValue..Uint64.MaxValue range
Declaration
public ulong NextRandomUnsignedLong { get; }
Property Value
| Type |
Description |
| System.UInt64 |
|
Methods
Introduces external entropy into the generation sequence by adding a sample into the ring buffer.
Call this method from places that have true entropy values, i.e.
a network-related code may have good entropy sources in server applications.
External entropy sources may rely on user-dependent actions, i.e.:
number of bytes/requests received per second, dollar(or cent remainders) amount of purchases made (on a server),
zip codes of customers, IP addresses of site visitors, average noise level sampled on an open WAVE device(microphone),
mouse position (i.e. in GUI app) etc...
Declaration
public void FeedExternalEntropySample(int sample)
Parameters
| Type |
Name |
Description |
| System.Int32 |
sample |
|
Generates a random buffer of bytes
Declaration
public byte[] NextRandomBytes(int length)
Parameters
| Type |
Name |
Description |
| System.Int32 |
length |
|
Returns
| Type |
Description |
| System.Byte[] |
|
Generates a random buffer of bytes
Declaration
public byte[] NextRandomBytes(int minLength, int maxLength)
Parameters
| Type |
Name |
Description |
| System.Int32 |
minLength |
|
| System.Int32 |
maxLength |
|
Returns
| Type |
Description |
| System.Byte[] |
|
Generates a random secure buffer of bytes
Declaration
public SecureBuffer NextRandomSecureBuffer(int length)
Parameters
| Type |
Name |
Description |
| System.Int32 |
length |
|
Returns
Generates a random secure buffer of bytes
Declaration
public SecureBuffer NextRandomSecureBuffer(int minLength, int maxLength)
Parameters
| Type |
Name |
Description |
| System.Int32 |
minLength |
|
| System.Int32 |
maxLength |
|
Returns
Generates a random secure buffer of chars which are safe for the use on the web -
a string that only contains "a-z"/"A-Z" and "0-9" and "-"/"_" chars, i.e.: "bo7O0EFasZe-wEty9w0__JiOKk81".
The length of the string can not be less than 4 and more than 1024 chars
Declaration
public SecureBuffer NextRandomWebSafeSecureBuffer(int minLength = 16, int maxLength = 32)
Parameters
| Type |
Name |
Description |
| System.Int32 |
minLength |
|
| System.Int32 |
maxLength |
|
Returns
Generates a random string of chars which are safe for the use on the web -
a string that only contains "a-z"/"A-Z" and "0-9" and "-"/"_" chars, i.e.: "bo7O0EFasZe-wEty9w0__JiOKk81".
The length of the string can not be less than 4 and more than 1024 chars
Declaration
public string NextRandomWebSafeString(int minLength = 16, int maxLength = 32)
Parameters
| Type |
Name |
Description |
| System.Int32 |
minLength |
|
| System.Int32 |
maxLength |
|
Returns
| Type |
Description |
| System.String |
|
Generates random double number in min..max range
Declaration
public double NextScaledRandomDouble(double bound1, double bound2 = 0)
Parameters
| Type |
Name |
Description |
| System.Double |
bound1 |
|
| System.Double |
bound2 |
|
Returns
| Type |
Description |
| System.Double |
|
Generates random number in min..max range
Declaration
public int NextScaledRandomInteger(int bound1, int bound2 = 0)
Parameters
| Type |
Name |
Description |
| System.Int32 |
bound1 |
|
| System.Int32 |
bound2 |
|
Returns
| Type |
Description |
| System.Int32 |
|
Extension Methods