KGy SOFT

RandomExtensions Class

KGy SOFT Core Libraries Help
Provides extension methods for the Random type.
See the Examples section for an example.
Inheritance Hierarchy

SystemObject
  KGySoft.CoreLibrariesRandomExtensions

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

public static class RandomExtensions

The RandomExtensions type exposes the following members.

Methods

  NameDescription
Public methodStatic memberNextBigInteger(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.
Public methodStatic memberNextBigInteger(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.
Public methodStatic memberNextBoolean
Returns a random Boolean value.
Public methodStatic memberNextByte(Random)
Returns a random 8-bit unsigned integer that is less than Byte.MaxValue. To return any Byte use the SampleByte method instead.
Public methodStatic memberNextByte(Random, Byte, Boolean)
Returns a random 8-bit unsigned integer that is within the specified maximum.
Public methodStatic memberNextByte(Random, Byte, Byte, Boolean)
Returns a random 8-bit unsigned integer that is within a specified range.
Public methodStatic memberNextBytes
Returns an Array of random bytes that has the specified length.
Public methodStatic memberNextChar
Returns a random Char value that is within a specified range.
Public methodStatic memberNextChars(Random, Char, StringCreation)
Fills the elements of a buffer of character array with random characters using the specified strategy.
Public methodStatic memberNextChars(Random, Char, Char)
Fills the elements of a buffer of character array with random characters using the specified allowedCharacters.
Public methodStatic memberNextChars(Random, Char, String)
Fills the elements of a buffer of character array with random characters using the specified allowedCharacters.
Public methodStatic memberNextChars(Random, Int32, StringCreation)
Returns an Array of random characters using the specified strategy that has the specified length.
Public methodStatic memberNextChars(Random, Int32, Char)
Returns an Array of random characters that has the specified length.
Public methodStatic memberNextChars(Random, Int32, ReadOnlySpanChar)
Returns an Array of random characters that has the specified length.
Public methodStatic memberNextChars(Random, Int32, String)
Returns an Array of random characters that has the specified length.
Public methodStatic memberNextChars(Random, SpanChar, StringCreation)
Fills the elements of a buffer with random characters using the specified strategy.
Public methodStatic memberNextChars(Random, SpanChar, ReadOnlySpanChar)
Fills the elements of a buffer with random characters using the specified allowedCharacters.
Public methodStatic memberNextDate
Returns a random DateTime that is between the specified range and has only date component.
Public methodStatic memberNextDateOnly
Returns a random DateOnly that is between the specified range.
Public methodStatic memberNextDateTime
Returns a random DateTime that is between the specified range.
Public methodStatic memberNextDateTimeOffset
Returns a random DateTimeOffset that is between the specified range.
Public methodStatic memberNextDecimal(Random)
Returns a random Decimal value that is greater than or equal to 0.0 and less than 1.0.
Public methodStatic memberNextDecimal(Random, Decimal, FloatScale)
Returns a non-negative random Decimal value that is less or equal to the specified maxValue.
See the Remarks section for details.
Public methodStatic memberNextDecimal(Random, Decimal, Decimal, FloatScale)
Returns a random Decimal value that is within a specified range.
See the Remarks section for details.
Public methodStatic memberNextDouble(Random, Double, FloatScale)
Returns a non-negative random Double value that is less or equal to the specified maxValue.
See the Remarks section for details.
Public methodStatic memberNextDouble(Random, Double, Double, FloatScale)
Returns a random Double value that is within a specified range.
See the Remarks section for details.
Public methodStatic memberNextEnumTEnum
Returns a random TEnum value.
Public methodStatic memberNextGuid
Returns a random RFC 4122 compliant Guid value generated by using the specified Random instance.
Public methodStatic memberNextHalf(Random)
Returns a random Half value that is greater than or equal to 0.0 and less than 1.0.
Public methodStatic memberNextHalf(Random, Half, FloatScale)
Returns a non-negative random Half value that is less or equal to the specified maxValue.
See the Remarks section for details.
Public methodStatic memberNextHalf(Random, Half, Half, FloatScale)
Returns a random Half value that is within a specified range.
See the Remarks section for details.
Public methodStatic memberNextInt16(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.
Public methodStatic memberNextInt16(Random, Int16, Boolean)
Returns a non-negative random 16-bit signed integer that is within the specified maximum.
Public methodStatic memberNextInt16(Random, Int16, Int16, Boolean)
Returns a random 16-bit signed integer that is within a specified range.
Public methodStatic memberNextInt32(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.
Public methodStatic memberNextInt32(Random, Int32, Boolean)
Returns a non-negative random 32-bit signed integer that is within the specified maximum.
Public methodStatic memberNextInt32(Random, Int32, Int32, Boolean)
Returns a random 32-bit signed integer that is within a specified range.
Public methodStatic memberNextInt64(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.
Public methodStatic memberNextInt64(Random, Int64, Boolean)
Returns a non-negative random 64-bit signed integer that is within the specified maximum.
Public methodStatic memberNextInt64(Random, Int64, Int64, Boolean)
Returns a random Int64 value that is within a specified range.
Public methodStatic memberNextObject(Random, Type, GenerateObjectSettings)
Returns a random object of the specified type or   if type cannot be instantiated with the provided settings.
Public methodStatic memberNextObjectT(Random, GenerateObjectSettings)
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.
Public methodStatic memberNextRune(Random)
Returns a random Rune (Unicode character).
Public methodStatic memberNextRune(Random, UnicodeCategory)
Returns a random Rune (Unicode character) that is within the specified category.
Public methodStatic memberNextRune(Random, Rune, Rune)
Returns a random Rune (Unicode character) that is within a specified range.
Public methodStatic memberNextSByte(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.
Public methodStatic memberNextSByte(Random, SByte, Boolean)
Returns a non-negative random 8-bit signed integer that is within the specified maximum.
Public methodStatic memberNextSByte(Random, SByte, SByte, Boolean)
Returns a random 8-bit signed integer that is within a specified range.
Public methodStatic memberNextSingle(Random)
Returns a random Single value that is greater than or equal to 0.0 and less than 1.0.
Public methodStatic memberNextSingle(Random, Single, FloatScale)
Returns a non-negative random Single value that is less or equal to the specified maxValue.
See the Remarks section for details.
Public methodStatic memberNextSingle(Random, Single, Single, FloatScale)
Returns a random Single value that is within a specified range.
See the Remarks section for details.
Public methodStatic memberNextString(Random, StringCreation)
Returns a random String using the specified strategy that has the length between 4 and 10 inclusive.
Public methodStatic memberNextString(Random, Int32, StringCreation)
Returns a random String of the specified length using the specified strategy that has the length between the specified range.
Public methodStatic memberNextString(Random, Int32, Int32, StringCreation)
Returns a random String using the specified strategy that has the length between the specified range.
Public methodStatic memberNextString(Random, Int32, Int32, Char)
Returns a random String that has the length between the specified range and consists of the specified allowedCharacters.
Public methodStatic memberNextString(Random, Int32, Int32, ReadOnlySpanChar)
Returns a random String that has the length between the specified range and consists of the specified allowedCharacters.
Public methodStatic memberNextString(Random, Int32, Int32, String)
Returns a random String that has the length between the specified range and consists of the specified allowedCharacters.
Public methodStatic memberNextTimeOnly
Returns a random TimeOnly that is between the specified range.
Public methodStatic memberNextTimeSpan
Returns a random TimeSpan that is between the specified range.
Public methodStatic memberNextUInt16(Random)
Returns a random 16-bit unsigned integer that is less than UInt16.MaxValue. To return any UInt16 use the SampleUInt16 method instead.
Public methodStatic memberNextUInt16(Random, UInt16, Boolean)
Returns a random 16-bit unsigned integer that is within the specified maximum.
Public methodStatic memberNextUInt16(Random, UInt16, UInt16, Boolean)
Returns a random 16-bit unsigned integer that is within a specified range.
Public methodStatic memberNextUInt32(Random)
Returns a random 32-bit unsigned integer that is less than UInt32.MaxValue. To return any UInt32 use the SampleUInt32 method instead.
Public methodStatic memberNextUInt32(Random, UInt32, Boolean)
Returns a random 32-bit unsigned integer that is within the specified maximum.
Public methodStatic memberNextUInt32(Random, UInt32, UInt32, Boolean)
Returns a random 32-bit unsigned integer that is within a specified range.
Public methodStatic memberNextUInt64(Random)
Returns a random 64-bit unsigned integer that is less than UInt64.MaxValue. To return any UInt64 use the SampleUInt64 method instead.
Public methodStatic memberNextUInt64(Random, UInt64, Boolean)
Returns a random 64-bit unsigned integer that is within the specified maximum.
Public methodStatic memberNextUInt64(Random, UInt64, UInt64, Boolean)
Returns a random 64-bit unsigned integer that is within a specified range.
Public methodStatic memberSampleBigInteger
Returns a random BigInteger that represents an integer of byteSize bytes.
Public methodStatic memberSampleByte
Returns a random Byte that can have any value.
Public methodStatic memberSampleInt16
Returns a random Int16 that can have any value.
Public methodStatic memberSampleInt32
Returns a random Int32 that can have any value.
Public methodStatic memberSampleInt64
Returns a random Int64 that can have any value.
Public methodStatic memberSampleSByte
Returns a random SByte that can have any value.
Public methodStatic memberSampleUInt16
Returns a random UInt16 that can have any value.
Public methodStatic memberSampleUInt32
Returns a random UInt32 that can have any value.
Public methodStatic memberSampleUInt64
Returns a random UInt64 that can have any value.
Top
Examples

Tip Tip
Try also online.
C#
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 *
See Also

Reference