A light weight C# extension of random method for all numeric type.
C# .NET 4.5 above
Chose one in the following:
- Search nuget for "RandomExtension"
- Run the following command in the Package Manager Console:
PM> Install-Package RandomExtension
- Download RandomExtension.dll and add referance to project.
- Download RandomExtension.cs file and add it into project.(C# 6 above)
using System.RandomExtension;
All extension method can be use like default method of System.Random, for example:
Random rnd = new Random();
//Random rnd = new Random(seed); //with seed
int randomInt = rnd.Next();
ulong randomULong = rnd.NextULong();
sbyte randomSByte = rnd.NextSByte(50);
decimal randomDecimal = rnd.NextDecimal(-3.5m, 8.55m);
For more detail, see document in Document of All Method section below.
- public double NextDouble(double minValue, double maxValue)
- public float NextFloat()
- public float NextFloat(float minValue, float maxValue)
- public decimal NextDecimal()
- public decimal NextDecimal(decimal minValue, decimal maxValue)
- public byte NextByte()
- public byte NextByte(byte maxValue)
- public byte NextByte(byte minValue, byte maxValue)
- public sbyte NextSByte()
- public sbyte NextSByte(sbyte maxValue)
- public sbyte NextSByte(sbyte minValue, sbyte maxValue)
- public short NextShort()
- public short NextShort(short maxValue)
- public short NextShort(short minValue, short maxValue)
- public ushort NextUShort()
- public ushort NextUShort(ushort maxValue)
- public ushort NextUShort(ushort minValue, ushort maxValue)
- public uint NextUInt()
- public uint NextUInt(uint maxValue)
- public uint NextUInt(uint minValue, uint maxValue)
- public long NextLong()
- public long NextLong(long maxValue)
- public long NextLong(long minValue, long maxValue)
- public ulong NextULong()
- public ulong NextULong(ulong maxValue)
- public ulong NextULong(ulong minValue, ulong maxValue)
For custom random algorithm or behavior, any class that inherit from System.Random can use these extension method too, and for those custom random class that override default random method(Next(), NextDouble(), NextBytes()), the behavior for method in this extension will also change, for example:
using System;
using System.RandomExtension;
namespace RandomExtensionExample
{
public class MyRandom : System.Random
{
public MyRandom() : base() { }
public MyRandom(int Seed) : base(Seed) { }
public override int Next(int minValue, int maxValue)
{
return 100;
}
}
public class Example
{
public static void Main()
{
MyRandom() rnd = new MyRandom();
//MyRandom rnd = new MyRandom(seed); //with seed
byte b = rnd.NextByte(0, 50); //100
}
}
}
public double NextDouble(
double minValue,
double maxValue
)
minValue
Type: System.Double
The inclusive lower bound of the random number returned.
maxValue
Type: System.Double
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.Double
A double-precision floating point number that is greater than or equal to minValue, and less than maxValue.
ArgumentOutOfRangeException
minValue is greater than maxValue.
The NextDouble(Double, Double) overload returns a random floating-point number that is greater than or equal to minValue, and less than maxValue. If maxValue equals minValue, the method returns minValue. NextDouble() from System.Random is used.
public float NextFloat()
Type: System.Single
A single-precision floating point number that is greater than or equal to 0.0f, and less than 1.0f.
The NextFloat() extend method returns a random floating-point number that is greater than or equal to 0.0f, and less than 1.0f. NextDouble() from System.Random is used. To retrieve random floating point values within a range other than 0.0f and 1.0f, use the NextFloat(Float, Float) method overload.
public float NextFloat(
float minValue,
float maxValue
)
minValue
Type: System.Single
The inclusive lower bound of the random number returned.
maxValue
Type: System.Single
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.Single
A single-precision floating point number that is greater than or equal to minValue, and less than maxValue.
ArgumentOutOfRangeException
minValue is greater than maxValue.
The NextFloat(Float, Float) overload returns a random floating-point number that is greater than or equal to minValue, and less than maxValue. If maxValue equals minValue, the method returns minValue. NextDouble() from System.Random is used.
public decimal NextDecimal()
Type: System.Decimal
A decimal number that is greater than or equal to 0.0m, and less than 1.0m.
The NextDecimal() extend method returns a random decimal number that is greater than or equal to 0.0m, and less than 1.0m. Next(Int32) from System.Random is used. To retrieve random decimal values within a range other than 0.0m and 1.0m, use the NextDecimal(Decimal, Decimal) method overload.
public decimal NextDecimal(
decimal minValue,
decimal maxValue
)
minValue
Type: System.Decimal
The inclusive lower bound of the random number returned.
maxValue
Type: System.Decimal
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.Decimal
A decimal number that is greater than or equal to minValue, and less than maxValue.
ArgumentOutOfRangeException
minValue is greater than maxValue.
The NextDecimal(Decimal, Decimal) overload returns a random decimal number that is greater than or equal to minValue, and less than maxValue. If maxValue equals minValue, the method returns minValue. Next(Int32) from System.Random is used.
public byte NextByte()
Type: System.Byte
A 8-bit unsigned integer that is greater than or equal to 0 and less than MaxValue.
The NextByte() extend method returns a random byte number that is range from 0 to Byte.MaxValue-1. Next(Int32) from System.Random is used. To generate a random byte number whose value ranges from 0 to some other byte number, use the NextByte(Byte) method overload. To generate a random byte number within a different range, use the NextByte(Byte, Byte) method overload.
public byte NextByte(
byte maxValue
)
maxValue
Type: System.Byte
The exclusive upper bound of the random number returned.
Type: System.Byte
A 8-bit unsigned integer that is greater than or equal to 0 and less than maxValue; that is, the range of return values ordinarily inclueds 0 but not maxValue. However, if maxValue equals 0, maxValue is return.
The NextByte(Byte) overload returns a random byte number that is range from 0 to maxValue-1. If maxValue is 0, the method returns 0. Next(Int32) from System.Random is used.
public byte NextByte(
byte minValue,
byte maxValue
)
minValue
Type: System.Byte
The inclusive lower bound of the random number returned.
maxValue
Type: System.Byte
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.Byte
A 8-bit unsigned integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.
ArgumentOutOfRangeException
minValue is greater than maxValue.
The NextByte(Byte, Byte) overload returns a random byte number that is range from minValue to maxValue. If maxValue equals minValue, the method returns minValue. Next(Int32, Int32) from System.Random is used.
public sbyte NextSByte()
Type: System.SByte
A 8-bit signed integer that is greater than or equal to 0 and less than MaxValue.
The NextSByte() extend method returns a random sbyte number that is range from 0 to SByte.MaxValue-1. Next(Int32) from System.Random is used. To generate a random sbyte number whose value ranges from 0 to some other positive sbyte number, use the NextSByte(SByte) method overload. To generate a random sbyte number within a different range, use the NextSByte(SByte, SByte) method overload.
public sbyte NextSByte(
sbyte maxValue
)
maxValue
Type: System.SByte
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to 0.
Type: System.SByte
A 8-bit signed integer that is greater than or equal to 0 and less than maxValue; that is, the range of return values ordinarily inclueds 0 but not maxValue. However, if maxValue equals 0, maxValue is return.
ArgumentOutOfRangeException
maxValue is less than 0.
The NextSByte(SByte) overload returns a random sbyte number that is range from 0 to maxValue-1. If maxValue is 0, the method returns 0. Next(Int32) from System.Random is used.
public sbyte NextSByte(
sbyte minValue,
sbyte maxValue
)
minValue
Type: System.SByte
The inclusive lower bound of the random number returned.
maxValue
Type: System.SByte
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.SByte
A 8-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.
ArgumentOutOfRangeException
minValue is greater than maxValue.
The NextSByte(SByte, SByte) overload returns a random sbyte number that is range from minValue to maxValue. If maxValue equals minValue, the method returns minValue. Next(Int32, Int32) from System.Random is used.
public short NextShort()
Type: System.Int16
A 16-bit signed integer that is greater than or equal to 0 and less than MaxValue.
The NextShort() extend method returns a random short number that is range from 0 to Int16.MaxValue-1. Next(Int32) from System.Random is used. To generate a random short number whose value ranges from 0 to some other positive short number, use the NextShort(Int16) method overload. To generate a random short number within a different range, use the NextShort(Int16, Int16) method overload.
public short NextShort(
short maxValue
)
maxValue
Type: System.Int16
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to 0.
Type: System.Int16
A 16-bit signed integer that is greater than or equal to 0 and less than maxValue; that is, the range of return values ordinarily inclueds 0 but not maxValue. However, if maxValue equals 0, maxValue is return.
ArgumentOutOfRangeException
maxValue is less than 0.
The NextShort(Int16) overload returns a random short number that is range from 0 to maxValue-1. If maxValue is 0, the method returns 0. Next(Int32) from System.Random is used.
public short NextShort(
short minValue,
short maxValue
)
minValue
Type: System.Int16
The inclusive lower bound of the random number returned.
maxValue
Type: System.Int16
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.Int16
A 16-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.
ArgumentOutOfRangeException
minValue is greater than maxValue.
The NextShort(Int16, Int16) overload returns a random short number that is range from minValue to maxValue. If maxValue equals minValue, the method returns minValue. Next(Int32, Int32) from System.Random is used.
public ushort NextUShort()
Type: System.UInt16
A 16-bit unsigned integer that is greater than or equal to 0 and less than MaxValue.
The NextUShort() extend method returns a random ushort number that is range from 0 to UInt16.MaxValue-1. Next(Int32) from System.Random is used. To generate a random ushort number whose value ranges from 0 to some other ushort number, use the NextUShort(UInt16) method overload. To generate a random ushort number within a different range, use the NextUShort(UInt16, UInt16) method overload.
public ushort NextUShort(
ushort maxValue
)
maxValue
Type: System.UInt16
The exclusive upper bound of the random number returned.
Type: System.UInt16
A 16-bit unsigned integer that is greater than or equal to 0 and less than maxValue; that is, the range of return values ordinarily inclueds 0 but not maxValue. However, if maxValue equals 0, maxValue is return.
The NextUShort(UInt16) overload returns a random ushort number that is range from 0 to maxValue-1. If maxValue is 0, the method returns 0. Next(Int32) from System.Random is used.
public ushort NextUShort(
ushort minValue,
ushort maxValue
)
minValue
Type: System.UInt16
The inclusive lower bound of the random number returned.
maxValue
Type: System.UInt16
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.UInt16
A 16-bit unsigned integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.
ArgumentOutOfRangeException
minValue is greater than maxValue.
The NextUShort(UInt16, UInt16) overload returns a random ushort number that is range from minValue to maxValue. If maxValue equals minValue, the method returns minValue. Next(Int32, Int32) from System.Random is used.
public uint NextUInt()
Type: System.UInt32
A 32-bit unsigned integer that is greater than or equal to 0 and less than MaxValue.
The NextUInt() extend method returns a random uint number that is range from 0 to UInt32.MaxValue-1. NextBytes(Byte[]) from System.Random is used. To generate a random uint number whose value ranges from 0 to some other uint number, use the NextUInt(UInt32) method overload. To generate a random uint number within a different range, use the NextUInt(UInt32, UInt32) method overload.
public uint NextUInt(
uint maxValue
)
maxValue
Type: System.UInt32
The exclusive upper bound of the random number returned.
Type: System.UInt32
A 32-bit unsigned integer that is greater than or equal to 0 and less than maxValue; that is, the range of return values ordinarily inclueds 0 but not maxValue. However, if maxValue equals 0, maxValue is return.
The NextUInt(UInt32) overload returns a random uint number that is range from 0 to maxValue-1. If maxValue is 0, the method returns 0. NextBytes(Byte[]) from System.Random is used.
public uint NextUInt(
uint minValue,
uint maxValue
)
minValue
Type: System.UInt32
The inclusive lower bound of the random number returned.
maxValue
Type: System.UInt32
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.UInt32
A 32-bit unsigned integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.
ArgumentOutOfRangeException
minValue is greater than maxValue.
The NextUInt(UInt32, UInt32) overload returns a random uint number that is range from minValue to maxValue. If maxValue equals minValue, the method returns minValue. NextBytes(Byte[]) from System.Random is used.
public long NextLong()
Type: System.Int64
A 64-bit signed integer that is greater than or equal to 0 and less than MaxValue.
The NextLong() extend method returns a random long number that is range from 0 to Int64.MaxValue-1. NextBytes(Byte[]) from System.Random is used. To generate a random long number whose value ranges from 0 to some other positive long number, use the NextLong(Int64) method overload. To generate a random long number within a different range, use the NextLong(Int64, Int64) method overload.
public long NextLong(
long maxValue
)
maxValue
Type: System.Int64
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to 0.
Type: System.Int64
A 64-bit signed integer that is greater than or equal to 0 and less than maxValue; that is, the range of return values ordinarily inclueds 0 but not maxValue. However, if maxValue equals 0, maxValue is return.
ArgumentOutOfRangeException
maxValue is less than 0.
The NextLong(Int64) overload returns a random long number that is range from 0 to maxValue-1. If maxValue is 0, the method returns 0. NextBytes(Byte[]) from System.Random is used.
public long NextLong(
long minValue,
long maxValue
)
minValue
Type: System.Int64
The inclusive lower bound of the random number returned.
maxValue
Type: System.Int64
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.Int64
A 64-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.
ArgumentOutOfRangeException
minValue is greater than maxValue.
The NextLong(Int64, Int64) overload returns a random long number that is range from minValue to maxValue. If maxValue equals minValue, the method returns minValue. Next(Int64, Int64) from System.Random is used.
public ulong NextULong()
Type: System.UInt64
A 64-bit unsigned integer that is greater than or equal to 0 and less than MaxValue.
The NextULong() extend method returns a random ulong number that is range from 0 to UInt64.MaxValue-1. NextBytes(Byte[]) from System.Random is used. To generate a random ulong number whose value ranges from 0 to some other ulong number, use the NextULong(UInt64) method overload. To generate a random ulong number within a different range, use the NextULong(UInt64, UInt64) method overload.
public ulong NextULong(
ulong maxValue
)
maxValue
Type: System.UInt64
The exclusive upper bound of the random number returned.
Type: System.UInt64
A 64-bit unsigned integer that is greater than or equal to 0 and less than maxValue; that is, the range of return values ordinarily inclueds 0 but not maxValue. However, if maxValue equals 0, maxValue is return.
The NextULong(UInt64) overload returns a random ulong number that is range from 0 to maxValue-1. If maxValue is 0, the method returns 0. NextBytes(Byte[]) from System.Random is used.
public ulong NextULong(
ulong minValue,
ulong maxValue
)
minValue
Type: System.UInt64
The inclusive lower bound of the random number returned.
maxValue
Type: System.UInt64
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.UInt64
A 64-bit unsigned integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.
ArgumentOutOfRangeException
minValue is greater than maxValue.
The NextULong(UInt64, UInt64) overload returns a random ulong number that is range from minValue to maxValue. If maxValue equals minValue, the method returns minValue. NextBytes(Byte[]) from System.Random is used.