Tip
Try also online.
public static class RandomExtensions
<ExtensionAttribute>
Public NotInheritable Class RandomExtensions
[ExtensionAttribute]
public ref class RandomExtensions abstract sealed
[<AbstractClassAttribute>]
[<SealedAttribute>]
[<ExtensionAttribute>]
type RandomExtensions = class end
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using KGySoft.CoreLibraries;
public static class Example
{
public static void Main()
{
// Or FastRandom for the fastest results, or SecureRandom for cryptographically safe results.
var rnd = new Random();
// Next... for all simple types:
Console.WriteLine(rnd.NextBoolean());
Console.WriteLine(rnd.NextDouble(Double.PositiveInfinity)); // see also the overloads
Console.WriteLine(rnd.NextString()); // see also the overloads
Console.WriteLine(rnd.NextDateTime()); // also NextDate, NextDateTimeOffset, NextTimeSpan
Console.WriteLine(rnd.NextEnum<ConsoleColor>());
// and NextByte, NextSByte, NextInt16, NextDecimal, etc.
// NextObject: for practically anything. See also GenerateObjectSettings.
Console.WriteLine(rnd.NextObject<Person>().Dump()); // custom type
Console.WriteLine(rnd.NextObject<(int, string)>()); // tuple
Console.WriteLine(rnd.NextObject<IConvertible>()); // interface implementation
Console.WriteLine(rnd.NextObject<MarshalByRefObject>()); // abstract type implementation
Console.WriteLine(rnd.NextObject<int[]>().Dump()); // array
Console.WriteLine(rnd.NextObject<IList<IConvertible>>().Dump()); // some collection of an interface
Console.WriteLine(rnd.NextObject<Func<DateTime>>().Invoke()); // delegate with random result
// specific type for object (useful for non-generic collections)
Console.WriteLine(rnd.NextObject<ArrayList>(new GenerateObjectSettings
{
SubstitutionForObjectType = typeof(ConsoleColor)
}).Dump());
// literally any random object
Console.WriteLine(rnd.NextObject<object>(new GenerateObjectSettings
{
AllowDerivedTypesForNonSealedClasses = true
})/*.Dump()*/); // dump may end up in an exception for property getters or even in an endless recursion
}
private static string Dump(this object o)
{
if (o == null)
return "<null>";
if (o is IConvertible convertible)
return convertible.ToString(CultureInfo.InvariantCulture);
if (o is IEnumerable enumerable)
return $"[{String.Join(", ", enumerable.Cast<object>().Select(Dump))}]";
return $"{{{String.Join("; ", o.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Select(p => $"{p.Name} = {Dump(p.GetValue(o))}"))}}}";
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public List<string> PhoneNumbers { get; set; }
}
// A possible output of the code above can be the following:
// False
// 1,65543763243888E+266
// }\&qc54# d
// 8806. 02. 18. 6:25:21
// White
// {FirstName = Jemp; LastName = Aeltep; BirthDate = 07/04/2003 00:00:00; PhoneNumbers = [17251827, 7099649772]}
// (1168349297, oufos)
// Renegotiated
// System.Net.Sockets.NetworkStream
// [336221768]
// [Off, Invalid]
// 1956. 08. 24. 4:28:57
// [Yellow, Gray]
// System.Xml.XmlCharCheckingReader+<ReadElementContentAsBinHexAsync>d__40 *
NextBigInteger(Random, BigInteger, Boolean) | Returns a non-negative random BigInteger that is within the specified maximum. To generate a random n-byte integer use the SampleBigInteger method instead. |
NextBigInteger(Random, BigInteger, BigInteger, Boolean) | Returns a random BigInteger value that is within a specified range. To generate a random n-byte integer use the SampleBigInteger method instead. |
NextBoolean | Returns a random Boolean value. |
NextByte(Random) | Returns a random 8-bit unsigned integer that is less than Byte.MaxValue. To return any Byte use the SampleByte method instead. |
NextByte(Random, Byte, Boolean) | Returns a random 8-bit unsigned integer that is within the specified maximum. |
NextByte(Random, Byte, Byte, Boolean) | Returns a random 8-bit unsigned integer that is within a specified range. |
NextBytes | Returns an Array of random bytes that has the specified length. |
NextChar | Returns a random Char value that is within a specified range. |
NextChars(Random, Char, StringCreation) | Fills the elements of a buffer of character array with random characters using the specified strategy. |
NextChars(Random, Char, Char) | Fills the elements of a buffer of character array with random characters using the specified allowedCharacters. |
NextChars(Random, Char, String) | Fills the elements of a buffer of character array with random characters using the specified allowedCharacters. |
NextChars(Random, Int32, StringCreation) | Returns an Array of random characters using the specified strategy that has the specified length. |
NextChars(Random, Int32, Char) | Returns an Array of random characters that has the specified length. |
NextChars(Random, Int32, ReadOnlySpanChar) | Returns an Array of random characters that has the specified length. |
NextChars(Random, Int32, String) | Returns an Array of random characters that has the specified length. |
NextChars(Random, SpanChar, StringCreation) | Fills the elements of a buffer with random characters using the specified strategy. |
NextChars(Random, SpanChar, ReadOnlySpanChar) | Fills the elements of a buffer with random characters using the specified allowedCharacters. |
NextDate | Returns a random DateTime that is between the specified range and has only date component. |
NextDateOnly | Returns a random DateOnly that is between the specified range. |
NextDateTime | Returns a random DateTime that is between the specified range. |
NextDateTimeOffset | Returns a random DateTimeOffset that is between the specified range. |
NextDecimal(Random) | Returns a random Decimal value that is greater than or equal to 0.0 and less than 1.0. |
NextDecimal(Random, Decimal, FloatScale) | Returns a non-negative random Decimal value that is less than or equal to the specified maxValue. |
NextDecimal(Random, Decimal, Decimal, FloatScale) | Returns a random Decimal value that is within a specified range. |
NextDouble(Random, Double, FloatScale) | Returns a non-negative random Double value that is less than or equal to the specified maxValue. |
NextDouble(Random, Double, Double, FloatScale) | Returns a random Double value that is within a specified range. |
NextEnumTEnum | Returns a random TEnum value. |
NextGuid | Returns a random RFC 4122 compliant Guid value generated by using the specified Random instance. |
NextHalf(Random) | Returns a random Half value that is greater than or equal to 0.0 and less than 1.0. |
NextHalf(Random, Half, FloatScale) | Returns a non-negative random Half value that is less than or equal to the specified maxValue. |
NextHalf(Random, Half, Half, FloatScale) | Returns a random Half value that is within a specified range. |
NextInt128(Random) | Returns a non-negative random 128-bit signed integer that is less than Int128.MaxValue. To return any Int128 use the SampleInt128 method instead. |
NextInt128(Random, Int128, Boolean) | Returns a non-negative random 128-bit signed integer that is within the specified maximum. |
NextInt128(Random, Int128, Int128, Boolean) | Returns a random Int128 value that is within a specified range. |
NextInt16(Random) | Returns a non-negative random 16-bit signed integer that is less than Int16.MaxValue. To return any Int16 use the SampleInt16 method instead. |
NextInt16(Random, Int16, Boolean) | Returns a non-negative random 16-bit signed integer that is within the specified maximum. |
NextInt16(Random, Int16, Int16, Boolean) | Returns a random 16-bit signed integer that is within a specified range. |
NextInt32(Random) | Returns a non-negative random 32-bit signed integer that is less than Int32.MaxValue. To return any Int32 use the SampleInt32 method instead. |
NextInt32(Random, Int32, Boolean) | Returns a non-negative random 32-bit signed integer that is within the specified maximum. |
NextInt32(Random, Int32, Int32, Boolean) | Returns a random 32-bit signed integer that is within a specified range. |
NextInt64(Random) | Returns a non-negative random 64-bit signed integer that is less than Int64.MaxValue. To return any Int64 use the SampleInt64 method instead. |
NextInt64(Random, Int64, Boolean) | Returns a non-negative random 64-bit signed integer that is within the specified maximum. |
NextInt64(Random, Int64, Int64, Boolean) | Returns a random Int64 value that is within a specified range. |
NextObject(Random, Type, GenerateObjectSettings) | Returns a random object of the specified type or if type cannot be instantiated with the provided settings. |
NextObjectT(Random, GenerateObjectSettings) | Returns a random object of type T or the default value of T if T cannot be instantiated with the provided settings. |
NextRune(Random) | Returns a random Rune (Unicode character). |
NextRune(Random, UnicodeCategory) | Returns a random Rune (Unicode character) that is within the specified category. |
NextRune(Random, Rune, Rune) | Returns a random Rune (Unicode character) that is within a specified range. |
NextSByte(Random) | Returns a non-negative random 8-bit signed integer that is less than SByte.MaxValue. To return any SByte use the SampleSByte method instead. |
NextSByte(Random, SByte, Boolean) | Returns a non-negative random 8-bit signed integer that is within the specified maximum. |
NextSByte(Random, SByte, SByte, Boolean) | Returns a random 8-bit signed integer that is within a specified range. |
NextSingle(Random) | Returns a random Single value that is greater than or equal to 0.0 and less than 1.0. |
NextSingle(Random, Single, FloatScale) | Returns a non-negative random Single value that is less than or equal to the specified maxValue. |
NextSingle(Random, Single, Single, FloatScale) | Returns a random Single value that is within a specified range. |
NextString(Random, StringCreation) | Returns a random String using the specified strategy that has the length between 4 and 10 inclusive. |
NextString(Random, Int32, StringCreation) | Returns a random String of the specified length using the specified strategy that has the length between the specified range. |
NextString(Random, Int32, Int32, StringCreation) | Returns a random String using the specified strategy that has the length between the specified range. |
NextString(Random, Int32, Int32, Char) | Returns a random String that has the length between the specified range and consists of the specified allowedCharacters. |
NextString(Random, Int32, Int32, ReadOnlySpanChar) | Returns a random String that has the length between the specified range and consists of the specified allowedCharacters. |
NextString(Random, Int32, Int32, String) | Returns a random String that has the length between the specified range and consists of the specified allowedCharacters. |
NextTimeOnly | Returns a random TimeOnly that is between the specified range. |
NextTimeSpan | Returns a random TimeSpan that is between the specified range. |
NextUInt128(Random) | Returns a random 128-bit unsigned integer that is less than UInt128.MaxValue. To return any UInt128 use the SampleUInt128 method instead. |
NextUInt128(Random, UInt128, Boolean) | Returns a random 128-bit unsigned integer that is within the specified maximum. |
NextUInt128(Random, UInt128, UInt128, Boolean) | Returns a random 128-bit unsigned integer that is within a specified range. |
NextUInt16(Random) | Returns a random 16-bit unsigned integer that is less than UInt16.MaxValue. To return any UInt16 use the SampleUInt16 method instead. |
NextUInt16(Random, UInt16, Boolean) | Returns a random 16-bit unsigned integer that is within the specified maximum. |
NextUInt16(Random, UInt16, UInt16, Boolean) | Returns a random 16-bit unsigned integer that is within a specified range. |
NextUInt32(Random) | Returns a random 32-bit unsigned integer that is less than UInt32.MaxValue. To return any UInt32 use the SampleUInt32 method instead. |
NextUInt32(Random, UInt32, Boolean) | Returns a random 32-bit unsigned integer that is within the specified maximum. |
NextUInt32(Random, UInt32, UInt32, Boolean) | Returns a random 32-bit unsigned integer that is within a specified range. |
NextUInt64(Random) | Returns a random 64-bit unsigned integer that is less than UInt64.MaxValue. To return any UInt64 use the SampleUInt64 method instead. |
NextUInt64(Random, UInt64, Boolean) | Returns a random 64-bit unsigned integer that is within the specified maximum. |
NextUInt64(Random, UInt64, UInt64, Boolean) | Returns a random 64-bit unsigned integer that is within a specified range. |
SampleBigInteger | Returns a random BigInteger that represents an integer of byteSize bytes. |
SampleByte | Returns a random Byte that can have any value. |
SampleInt128 | Returns a random Int128 that can have any value. |
SampleInt16 | Returns a random Int16 that can have any value. |
SampleInt32 | Returns a random Int32 that can have any value. |
SampleInt64 | Returns a random Int64 that can have any value. |
SampleSByte | Returns a random SByte that can have any value. |
SampleUInt128 | Returns a random UInt128 that can have any value. |
SampleUInt16 | Returns a random UInt16 that can have any value. |
SampleUInt32 | Returns a random UInt32 that can have any value. |
SampleUInt64 | Returns a random UInt64 that can have any value. |