KGy SOFT

ThreadSafeRandom Class

KGy SOFT Core Libraries Help
Represents a thread-safe wrapper for random number generators. You can use the static Create methods to create a customized instance (eg. you can wrap a SecureRandom instance to generate cryptographically safe random numbers in a thread-safe way), or just use the static Instance property for a fast shared instance (which uses FastRandom internally).
Inheritance Hierarchy

SystemObject
  SystemRandom
    KGySoft.CoreLibrariesThreadSafeRandom

Namespace:  KGySoft.CoreLibraries
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 6.0.0
Syntax

public class ThreadSafeRandom : Random, 
	IDisposable

The ThreadSafeRandom type exposes the following members.

Constructors

  NameDescription
Public methodThreadSafeRandom Obsolete.
Initializes a new instance of the ThreadSafeRandom class, using a random seed value. It is practically the same as using the Instance property.
Public methodThreadSafeRandom(Int32) Obsolete.
Initializes a new instance of the ThreadSafeRandom class using the specified seed value. It is practically the same as using the Create(Int32) method.
Top
Properties

  NameDescription
Public propertyStatic memberInstance
Gets a ThreadSafeRandom instance that can be used from any threads concurrently.
Top
Methods

  NameDescription
Public methodStatic memberCreate(FuncRandom)
Creates a ThreadSafeRandom instance using the specified factory in each thread the result is accessed from.
See the Remarks section for details.
Public methodStatic memberCreate(Int32)
Creates a ThreadSafeRandom instance using the specified seed value.
See the Remarks section for details.
Public methodDispose
Disposes this ThreadSafeRandom instance.
Protected methodDispose(Boolean)
Releases the resources used by this ThreadSafeRandom instance.
Public methodNext
Returns a non-negative random 32-bit integer that is less than Int32.MaxValue.
(Overrides RandomNext.)
Public methodNext(Int32)
Returns a non-negative random 32-bit integer that is less than the specified maximum.
(Overrides RandomNext(Int32).)
Public methodNext(Int32, Int32)
Returns a random 32-bit integer that is within a specified range.
(Overrides RandomNext(Int32, Int32).)
Public methodNextBytes(Byte)
Fills the elements of a specified array of bytes with random numbers.
(Overrides RandomNextBytes(Byte).)
Public methodNextBytes(SpanByte)
Fills the elements of the specified buffer with random numbers.
(Overrides RandomNextBytes(SpanByte).)
Public methodNextDouble
Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
(Overrides RandomNextDouble.)
Public methodNextInt64
Returns a non-negative random 64-bit integer that is less than Int64.MaxValue.
(Overrides Random.NextInt64.)
Public methodNextInt64(Int64)
Returns a non-negative random 64-bit integer that is less than the specified maximum.
(Overrides Random.NextInt64(Int64).)
Public methodNextInt64(Int64, Int64)
Returns a random 64-bit integer that is within a specified range.
(Overrides Random.NextInt64(Int64, Int64).)
Public methodNextSingle
Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
(Overrides Random.NextSingle.)
Protected methodSample
Returns a random floating-point number between 0.0 and 1.0.
(Overrides RandomSample.)
Top
Extension Methods

  NameDescription
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
See the Remarks section for details.
(Defined by ObjectExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodNextBigInteger(BigInteger, Boolean)Overloaded.
Returns a non-negative random BigInteger that is within the specified maximum. To generate a random n-byte integer use the SampleBigInteger method instead.
(Defined by RandomExtensions.)
Public Extension MethodNextBigInteger(BigInteger, BigInteger, Boolean)Overloaded.
Returns a random BigInteger value that is within a specified range. To generate a random n-byte integer use the SampleBigInteger method instead.
(Defined by RandomExtensions.)
Public Extension MethodNextBoolean
Returns a random Boolean value.
(Defined by RandomExtensions.)
Public Extension MethodNextByteOverloaded.
Returns a random 8-bit unsigned integer that is less than Byte.MaxValue. To return any Byte use the SampleByte method instead.
(Defined by RandomExtensions.)
Public Extension MethodNextByte(Byte, Boolean)Overloaded.
Returns a random 8-bit unsigned integer that is within the specified maximum.
(Defined by RandomExtensions.)
Public Extension MethodNextByte(Byte, Byte, Boolean)Overloaded.
Returns a random 8-bit unsigned integer that is within a specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextBytes
Returns an Array of random bytes that has the specified length.
(Defined by RandomExtensions.)
Public Extension MethodNextChar
Returns a random Char value that is within a specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextChars(Char, String)Overloaded.
Fills the elements of a buffer of character array with random characters using the specified allowedCharacters.
(Defined by RandomExtensions.)
Public Extension MethodNextChars(Char, Char)Overloaded.
Fills the elements of a buffer of character array with random characters using the specified allowedCharacters.
(Defined by RandomExtensions.)
Public Extension MethodNextChars(Char, StringCreation)Overloaded.
Fills the elements of a buffer of character array with random characters using the specified strategy.
(Defined by RandomExtensions.)
Public Extension MethodNextChars(Int32, String)Overloaded.
Returns an Array of random characters that has the specified length.
(Defined by RandomExtensions.)
Public Extension MethodNextChars(Int32, Char)Overloaded.
Returns an Array of random characters that has the specified length.
(Defined by RandomExtensions.)
Public Extension MethodNextChars(Int32, ReadOnlySpanChar)Overloaded.
Returns an Array of random characters that has the specified length.
(Defined by RandomExtensions.)
Public Extension MethodNextChars(Int32, StringCreation)Overloaded.
Returns an Array of random characters using the specified strategy that has the specified length.
(Defined by RandomExtensions.)
Public Extension MethodNextChars(SpanChar, ReadOnlySpanChar)Overloaded.
Fills the elements of a buffer with random characters using the specified allowedCharacters.
(Defined by RandomExtensions.)
Public Extension MethodNextChars(SpanChar, StringCreation)Overloaded.
Fills the elements of a buffer with random characters using the specified strategy.
(Defined by RandomExtensions.)
Public Extension MethodNextDate
Returns a random DateTime that is between the specified range and has only date component.
(Defined by RandomExtensions.)
Public Extension MethodNextDateOnly
Returns a random DateOnly that is between the specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextDateTime
Returns a random DateTime that is between the specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextDateTimeOffset
Returns a random DateTimeOffset that is between the specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextDecimalOverloaded.
Returns a random Decimal value that is greater than or equal to 0.0 and less than 1.0.
(Defined by RandomExtensions.)
Public Extension MethodNextDecimal(Decimal, FloatScale)Overloaded.
Returns a non-negative random Decimal value that is less or equal to the specified maxValue.
See the Remarks section for details.
(Defined by RandomExtensions.)
Public Extension MethodNextDecimal(Decimal, Decimal, FloatScale)Overloaded.
Returns a random Decimal value that is within a specified range.
See the Remarks section for details.
(Defined by RandomExtensions.)
Public Extension MethodNextDouble(Double, FloatScale)Overloaded.
Returns a non-negative random Double value that is less or equal to the specified maxValue.
See the Remarks section for details.
(Defined by RandomExtensions.)
Public Extension MethodNextDouble(Double, Double, FloatScale)Overloaded.
Returns a random Double value that is within a specified range.
See the Remarks section for details.
(Defined by RandomExtensions.)
Public Extension MethodNextEnumTEnum
Returns a random TEnum value.
(Defined by RandomExtensions.)
Public Extension MethodNextGuid
Returns a random RFC 4122 compliant Guid value generated by using the specified Random instance.
(Defined by RandomExtensions.)
Public Extension MethodNextHalfOverloaded.
Returns a random Half value that is greater than or equal to 0.0 and less than 1.0.
(Defined by RandomExtensions.)
Public Extension MethodNextHalf(Half, FloatScale)Overloaded.
Returns a non-negative random Half value that is less or equal to the specified maxValue.
See the Remarks section for details.
(Defined by RandomExtensions.)
Public Extension MethodNextHalf(Half, Half, FloatScale)Overloaded.
Returns a random Half value that is within a specified range.
See the Remarks section for details.
(Defined by RandomExtensions.)
Public Extension MethodNextInt16Overloaded.
Returns a non-negative random 16-bit signed integer that is less than Int16.MaxValue. To return any Int16 use the SampleInt16 method instead.
(Defined by RandomExtensions.)
Public Extension MethodNextInt16(Int16, Boolean)Overloaded.
Returns a non-negative random 16-bit signed integer that is within the specified maximum.
(Defined by RandomExtensions.)
Public Extension MethodNextInt16(Int16, Int16, Boolean)Overloaded.
Returns a random 16-bit signed integer that is within a specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextInt32Overloaded.
Returns a non-negative random 32-bit signed integer that is less than Int32.MaxValue. To return any Int32 use the SampleInt32 method instead.
(Defined by RandomExtensions.)
Public Extension MethodNextInt32(Int32, Boolean)Overloaded.
Returns a non-negative random 32-bit signed integer that is within the specified maximum.
(Defined by RandomExtensions.)
Public Extension MethodNextInt32(Int32, Int32, Boolean)Overloaded.
Returns a random 32-bit signed integer that is within a specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextInt64Overloaded.
Returns a non-negative random 64-bit signed integer that is less than Int64.MaxValue. To return any Int64 use the SampleInt64 method instead.
(Defined by RandomExtensions.)
Public Extension MethodNextInt64(Int64, Boolean)Overloaded.
Returns a non-negative random 64-bit signed integer that is within the specified maximum.
(Defined by RandomExtensions.)
Public Extension MethodNextInt64(Int64, Int64, Boolean)Overloaded.
Returns a random Int64 value that is within a specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextObject(Type, GenerateObjectSettings)Overloaded.
Returns a random object of the specified type or   if type cannot be instantiated with the provided settings.
(Defined by RandomExtensions.)
Public Extension MethodNextObjectT(GenerateObjectSettings)Overloaded.
Returns a random object of type T or the default value of T if T cannot be instantiated with the provided settings.
See the Remarks section for details.
(Defined by RandomExtensions.)
Public Extension MethodNextRuneOverloaded.
Returns a random Rune (Unicode character).
(Defined by RandomExtensions.)
Public Extension MethodNextRune(UnicodeCategory)Overloaded.
Returns a random Rune (Unicode character) that is within the specified category.
(Defined by RandomExtensions.)
Public Extension MethodNextRune(Rune, Rune)Overloaded.
Returns a random Rune (Unicode character) that is within a specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextSByteOverloaded.
Returns a non-negative random 8-bit signed integer that is less than SByte.MaxValue. To return any SByte use the SampleSByte method instead.
(Defined by RandomExtensions.)
Public Extension MethodNextSByte(SByte, Boolean)Overloaded.
Returns a non-negative random 8-bit signed integer that is within the specified maximum.
(Defined by RandomExtensions.)
Public Extension MethodNextSByte(SByte, SByte, Boolean)Overloaded.
Returns a random 8-bit signed integer that is within a specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextSingleOverloaded.
Returns a random Single value that is greater than or equal to 0.0 and less than 1.0.
(Defined by RandomExtensions.)
Public Extension MethodNextSingle(Single, FloatScale)Overloaded.
Returns a non-negative random Single value that is less or equal to the specified maxValue.
See the Remarks section for details.
(Defined by RandomExtensions.)
Public Extension MethodNextSingle(Single, Single, FloatScale)Overloaded.
Returns a random Single value that is within a specified range.
See the Remarks section for details.
(Defined by RandomExtensions.)
Public Extension MethodNextString(StringCreation)Overloaded.
Returns a random String using the specified strategy that has the length between 4 and 10 inclusive.
(Defined by RandomExtensions.)
Public Extension MethodNextString(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.)
Public Extension MethodNextString(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.)
Public Extension MethodNextString(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.)
Public Extension MethodNextString(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.)
Public Extension MethodNextString(Int32, Int32, StringCreation)Overloaded.
Returns a random String using the specified strategy that has the length between the specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextTimeOnly
Returns a random TimeOnly that is between the specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextTimeSpan
Returns a random TimeSpan that is between the specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextUInt16Overloaded.
Returns a random 16-bit unsigned integer that is less than UInt16.MaxValue. To return any UInt16 use the SampleUInt16 method instead.
(Defined by RandomExtensions.)
Public Extension MethodNextUInt16(UInt16, Boolean)Overloaded.
Returns a random 16-bit unsigned integer that is within the specified maximum.
(Defined by RandomExtensions.)
Public Extension MethodNextUInt16(UInt16, UInt16, Boolean)Overloaded.
Returns a random 16-bit unsigned integer that is within a specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextUInt32Overloaded.
Returns a random 32-bit unsigned integer that is less than UInt32.MaxValue. To return any UInt32 use the SampleUInt32 method instead.
(Defined by RandomExtensions.)
Public Extension MethodNextUInt32(UInt32, Boolean)Overloaded.
Returns a random 32-bit unsigned integer that is within the specified maximum.
(Defined by RandomExtensions.)
Public Extension MethodNextUInt32(UInt32, UInt32, Boolean)Overloaded.
Returns a random 32-bit unsigned integer that is within a specified range.
(Defined by RandomExtensions.)
Public Extension MethodNextUInt64Overloaded.
Returns a random 64-bit unsigned integer that is less than UInt64.MaxValue. To return any UInt64 use the SampleUInt64 method instead.
(Defined by RandomExtensions.)
Public Extension MethodNextUInt64(UInt64, Boolean)Overloaded.
Returns a random 64-bit unsigned integer that is within the specified maximum.
(Defined by RandomExtensions.)
Public Extension MethodNextUInt64(UInt64, UInt64, Boolean)Overloaded.
Returns a random 64-bit unsigned integer that is within a specified range.
(Defined by RandomExtensions.)
Public Extension MethodSampleBigInteger
Returns a random BigInteger that represents an integer of byteSize bytes.
(Defined by RandomExtensions.)
Public Extension MethodSampleByte
Returns a random Byte that can have any value.
(Defined by RandomExtensions.)
Public Extension MethodSampleInt16
Returns a random Int16 that can have any value.
(Defined by RandomExtensions.)
Public Extension MethodSampleInt32
Returns a random Int32 that can have any value.
(Defined by RandomExtensions.)
Public Extension MethodSampleInt64
Returns a random Int64 that can have any value.
(Defined by RandomExtensions.)
Public Extension MethodSampleSByte
Returns a random SByte that can have any value.
(Defined by RandomExtensions.)
Public Extension MethodSampleUInt16
Returns a random UInt16 that can have any value.
(Defined by RandomExtensions.)
Public Extension MethodSampleUInt32
Returns a random UInt32 that can have any value.
(Defined by RandomExtensions.)
Public Extension MethodSampleUInt64
Returns a random UInt64 that can have any value.
(Defined by RandomExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Top
See Also

Reference