Represents a secure random number generator, which uses a RandomNumberGenerator instance to produce
cryptographically secure random numbers.
This class is functionally compatible with the Random and FastRandom classes.
Inheritance Hierarchy
SystemRandom
KGySoft.Security.CryptographySecureRandom
Namespace: KGySoft.Security.Cryptography
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 6.0.0rc.2
Syntax
The SecureRandom type exposes the following members.
Constructors
Name  Description  

SecureRandom 
Initializes a new instance of the SecureRandom class.
To generate cryptographically secure random numbers, a default RandomNumberGenerator will be used internally.
 
SecureRandom(RandomNumberGenerator) 
Initializes a new instance of the SecureRandom class.

Methods
Name  Description  

Dispose 
Disposes the current SecureRandom instance.
 
Dispose(Boolean) 
Releases the resources used by this SecureRandom instance.
 
Next 
Returns a nonnegative random 32bit integer that is less than Int32.MaxValue.
(Overrides RandomNext.)  
Next(Int32) 
Returns a nonnegative random integer that is less than the specified maximum.
(Overrides RandomNext(Int32).)  
Next(Int32, Int32) 
Returns a random 32bit integer that is within a specified range.
(Overrides RandomNext(Int32, Int32).)  
NextBytes(Byte) 
Fills the elements of a specified array of bytes with random numbers.
(Overrides RandomNextBytes(Byte).)  
NextBytes(SpanByte) 
Fills the elements of the specified buffer with random numbers.
(Overrides RandomNextBytes(SpanByte).)  
NextDouble 
Returns a random floatingpoint number that is greater than or equal to 0.0, and less than 1.0.
(Overrides RandomNextDouble.)  
NextInt64 
Returns a nonnegative random 64bit integer that is less than Int64.MaxValue.
(Overrides Random.NextInt64.)  
NextInt64(Int64) 
Returns a nonnegative random 64bit integer that is less than the specified maximum.
(Overrides Random.NextInt64(Int64).)  
NextInt64(Int64, Int64) 
Returns a random 64bit integer that is within a specified range.
(Overrides Random.NextInt64(Int64, Int64).)  
NextSingle 
Returns a random floatingpoint number that is greater than or equal to 0.0, and less than 1.0.
(Overrides Random.NextSingle.)  
Sample 
Returns a random floatingpoint number between 0.0 and 1.0.
(Overrides RandomSample.) 
Extension Methods
Name  Description  

Convert(Type, CultureInfo)  Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.  
ConvertTTarget(CultureInfo)  Overloaded. (Defined by ObjectExtensions.)  
In 
Gets whether item is among the elements of set.
(Defined by ObjectExtensions.)See the Examples section of the generic InT(T, T) overload for an example.  
NextBoolean 
Returns a random Boolean value.
(Defined by RandomExtensions.)  
NextByte  Overloaded.
Returns a random 8bit unsigned integer that is less than Byte.MaxValue.
To return any Byte use the SampleByte method instead.
(Defined by RandomExtensions.)  
NextByte(Byte, Boolean)  Overloaded.
Returns a random 8bit unsigned integer that is within the specified maximum.
(Defined by RandomExtensions.)  
NextByte(Byte, Byte, Boolean)  Overloaded.
Returns a random 8bit unsigned integer that is within a specified range.
(Defined by RandomExtensions.)  
NextBytes 
Returns an Array of random bytes that has the specified length.
(Defined by RandomExtensions.)  
NextChar 
Returns a random Char value that is within a specified range.
(Defined by RandomExtensions.)  
NextChars(Int32, String)  Overloaded.
Returns an Array of random characters that has the specified length.
(Defined by RandomExtensions.)  
NextChars(Int32, Char)  Overloaded.
Returns an Array of random characters that has the specified length.
(Defined by RandomExtensions.)  
NextChars(Int32, ReadOnlySpanChar)  Overloaded.
Returns an Array of random characters that has the specified length.
(Defined by RandomExtensions.)  
NextChars(Int32, StringCreation)  Overloaded. (Defined by RandomExtensions.)  
NextChars(Char, String)  Overloaded.
Fills the elements of a buffer of character array with random characters using the specified allowedCharacters.
(Defined by RandomExtensions.)  
NextChars(Char, Char)  Overloaded.
Fills the elements of a buffer of character array with random characters using the specified allowedCharacters.
(Defined by RandomExtensions.)  
NextChars(Char, StringCreation)  Overloaded.
Fills the elements of a buffer of character array with random characters using the specified strategy.
(Defined by RandomExtensions.)  
NextChars(SpanChar, ReadOnlySpanChar)  Overloaded.
Fills the elements of a buffer with random characters using the specified allowedCharacters.
(Defined by RandomExtensions.)  
NextChars(SpanChar, StringCreation)  Overloaded.
Fills the elements of a buffer with random characters using the specified strategy.
(Defined by RandomExtensions.)  
NextDate 
Returns a random DateTime that is between the specified range and has only date component.
(Defined by RandomExtensions.)  
NextDateTime 
Returns a random DateTime that is between the specified range.
(Defined by RandomExtensions.)  
NextDateTimeOffset 
Returns a random DateTimeOffset that is between the specified range.
(Defined by RandomExtensions.)  
NextDecimal  Overloaded.
Returns a random Decimal value that is greater than or equal to 0.0 and less than 1.0.
(Defined by RandomExtensions.)  
NextDecimal(Decimal, FloatScale)  Overloaded.
Returns a nonnegative random Decimal value that is less or equal to the specified maxValue.
(Defined by RandomExtensions.)See the Remarks section for details.  
NextDecimal(Decimal, Decimal, FloatScale)  Overloaded.
Returns a random Decimal value that is within a specified range.
(Defined by RandomExtensions.)See the Remarks section for details.  
NextDouble(Double, FloatScale)  Overloaded.
Returns a nonnegative random Double value that is less or equal to the specified maxValue.
(Defined by RandomExtensions.)See the Remarks section for details.  
NextDouble(Double, Double, FloatScale)  Overloaded.
Returns a random Double value that is within a specified range.
(Defined by RandomExtensions.)See the Remarks section for details.  
NextEnumTEnum 
Returns a random TEnum value.
(Defined by RandomExtensions.)  
NextGuid  (Defined by RandomExtensions.)  
NextInt16  Overloaded.
Returns a nonnegative random 16bit signed integer that is less than Int16.MaxValue.
To return any Int16 use the SampleInt16 method instead.
(Defined by RandomExtensions.)  
NextInt16(Int16, Boolean)  Overloaded.
Returns a nonnegative random 16bit signed integer that is within the specified maximum.
(Defined by RandomExtensions.)  
NextInt16(Int16, Int16, Boolean)  Overloaded.
Returns a random 16bit signed integer that is within a specified range.
(Defined by RandomExtensions.)  
NextInt32  Overloaded.
Returns a nonnegative random 32bit signed integer that is less than Int32.MaxValue.
To return any Int32 use the SampleInt32 method instead.
(Defined by RandomExtensions.)  
NextInt32(Int32, Boolean)  Overloaded.
Returns a nonnegative random 32bit signed integer that is within the specified maximum.
(Defined by RandomExtensions.)  
NextInt32(Int32, Int32, Boolean)  Overloaded.
Returns a random 32bit signed integer that is within a specified range.
(Defined by RandomExtensions.)  
NextInt64  Overloaded.
Returns a nonnegative random 64bit signed integer that is less than Int64.MaxValue.
To return any Int64 use the SampleInt64 method instead.
(Defined by RandomExtensions.)  
NextInt64(Int64, Boolean)  Overloaded.
Returns a nonnegative random 64bit signed integer that is within the specified maximum.
(Defined by RandomExtensions.)  
NextInt64(Int64, Int64, Boolean)  Overloaded.
Returns a random Int64 value that is within a specified range.
(Defined by RandomExtensions.)  
NextObject(Type, GenerateObjectSettings)  Overloaded.
Returns a random object of the specified type or
if type cannot be instantiated with the provided settings.
(Defined by RandomExtensions.)  
NextObjectT(GenerateObjectSettings)  Overloaded.
Returns a random object of type T or the default value of T
if T cannot be instantiated with the provided settings.
(Defined by RandomExtensions.)See the Remarks section for details.  
NextSByte  Overloaded.
Returns a nonnegative random 8bit signed integer that is less than SByte.MaxValue.
To return any SByte use the SampleSByte method instead.
(Defined by RandomExtensions.)  
NextSByte(SByte, Boolean)  Overloaded.
Returns a nonnegative random 8bit signed integer that is within the specified maximum.
(Defined by RandomExtensions.)  
NextSByte(SByte, SByte, Boolean)  Overloaded.
Returns a random 8bit signed integer that is within a specified range.
(Defined by RandomExtensions.)  
NextSingle  Overloaded.
Returns a random Single value that is greater than or equal to 0.0 and less than 1.0.
(Defined by RandomExtensions.)  
NextSingle(Single, FloatScale)  Overloaded.
Returns a nonnegative random Single value that is less or equal to the specified maxValue.
(Defined by RandomExtensions.)See the Remarks section for details.  
NextSingle(Single, Single, FloatScale)  Overloaded.
Returns a random Single value that is within a specified range.
(Defined by RandomExtensions.)See the Remarks section for details.  
NextString(StringCreation)  Overloaded.
Returns a random String using the specified strategy that has the length between 4 and 10 inclusive.
(Defined by RandomExtensions.)  
NextString(Int32, StringCreation)  Overloaded.
Returns a random String of the specified length using the specified strategy that has the length between the specified range.
(Defined by RandomExtensions.)  
NextString(Int32, Int32, String)  Overloaded.
Returns a random String that has the length between the specified range and consists of the specified allowedCharacters.
(Defined by RandomExtensions.)  
NextString(Int32, Int32, Char)  Overloaded.
Returns a random String that has the length between the specified range and consists of the specified allowedCharacters.
(Defined by RandomExtensions.)  
NextString(Int32, Int32, ReadOnlySpanChar)  Overloaded.
Returns a random String that has the length between the specified range and consists of the specified allowedCharacters.
(Defined by RandomExtensions.)  
NextString(Int32, Int32, StringCreation)  Overloaded.
Returns a random String using the specified strategy that has the length between the specified range.
(Defined by RandomExtensions.)  
NextTimeSpan 
Returns a random TimeSpan that is between the specified range.
(Defined by RandomExtensions.)  
NextUInt16  Overloaded.
Returns a random 16bit unsigned integer that is less than UInt16.MaxValue.
To return any UInt16 use the SampleUInt16 method instead.
(Defined by RandomExtensions.)  
NextUInt16(UInt16, Boolean)  Overloaded.
Returns a random 16bit unsigned integer that is within the specified maximum.
(Defined by RandomExtensions.)  
NextUInt16(UInt16, UInt16, Boolean)  Overloaded.
Returns a random 16bit unsigned integer that is within a specified range.
(Defined by RandomExtensions.)  
NextUInt32  Overloaded.
Returns a random 32bit unsigned integer that is less than UInt32.MaxValue.
To return any UInt32 use the SampleUInt32 method instead.
(Defined by RandomExtensions.)  
NextUInt32(UInt32, Boolean)  Overloaded.
Returns a random 32bit unsigned integer that is within the specified maximum.
(Defined by RandomExtensions.)  
NextUInt32(UInt32, UInt32, Boolean)  Overloaded.
Returns a random 32bit unsigned integer that is within a specified range.
(Defined by RandomExtensions.)  
NextUInt64  Overloaded.
Returns a random 64bit unsigned integer that is less than UInt64.MaxValue.
To return any UInt64 use the SampleUInt64 method instead.
(Defined by RandomExtensions.)  
NextUInt64(UInt64, Boolean)  Overloaded.
Returns a random 64bit unsigned integer that is within the specified maximum.
(Defined by RandomExtensions.)  
NextUInt64(UInt64, UInt64, Boolean)  Overloaded.
Returns a random 64bit unsigned integer that is within a specified range.
(Defined by RandomExtensions.)  
SampleByte 
Returns a random Byte that can have any value.
(Defined by RandomExtensions.)  
SampleInt16 
Returns a random Int16 that can have any value.
(Defined by RandomExtensions.)  
SampleInt32 
Returns a random Int32 that can have any value.
(Defined by RandomExtensions.)  
SampleInt64 
Returns a random Int64 that can have any value.
(Defined by RandomExtensions.)  
SampleSByte 
Returns a random SByte that can have any value.
(Defined by RandomExtensions.)  
SampleUInt16 
Returns a random UInt16 that can have any value.
(Defined by RandomExtensions.)  
SampleUInt32 
Returns a random UInt32 that can have any value.
(Defined by RandomExtensions.)  
SampleUInt64 
Returns a random UInt64 that can have any value.
(Defined by RandomExtensions.)  
TryConvert(Type, Object)  Overloaded. (Defined by ObjectExtensions.)  
TryConvert(Type, CultureInfo, Object)  Overloaded. (Defined by ObjectExtensions.)  
TryConvertTTarget(TTarget)  Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.  
TryConvertTTarget(CultureInfo, TTarget)  Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. 
Remarks
Note 

Please note that SecureRandom class implements the IDisposable interface so make sure you dispose it (or use it in a using block) if not used in a static context. 
See Also