RandomExtensionsNextDecimal(Random, Decimal, Decimal, FloatScale) Method

Returns a random Decimal value that is within a specified range.


Namespace: KGySoft.CoreLibraries
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.1.0
public static decimal NextDecimal(
	this Random random,
	decimal minValue,
	decimal maxValue,
	FloatScale scale = FloatScale.Auto


random  Random
The Random instance to use.
minValue  Decimal
The lower bound of the random number returned.
maxValue  Decimal
The upper bound of the random number returned. Must be greater or equal to minValue.
scale  FloatScale  (Optional)
The scale to use to generate the random number. This parameter is optional.
Default value: Auto.

Return Value

A decimal floating point number that is greater than or equal to minValue and less than or equal to maxValue.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type Random. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).


In most cases return value is less than maxValue. Return value can be equal to maxValue in very edge cases such as when minValue is equal to maxValue or when the range is near Epsilon.

If scale is Auto, then the ForceLinear option is used if the absolute value of both minValue and maxValue are less than or equal to 65535, or when they have the same sign and the absolute value of maxValue is less than 16 times greater than the absolute value of minValue. For larger ranges the ForceLogarithmic option is used.

Generating random numbers by this method on the logarithmic scale is about 25-50 times slower than on the linear scale.


ArgumentNullExceptionrandom is .
ArgumentOutOfRangeExceptionmaxValue is less than minValue
scale is not a valid value of FloatScale.

See Also