KGy SOFT

RandomExtensions Class

KGy SOFT Core Libraries Help
Contains 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: 5.0.0-rc.1
Syntax

public static class RandomExtensions

The RandomExtensions type exposes the following members.

Methods

  NameDescription
Public methodStatic memberNextBoolean
Returns a random Boolean value.
Public methodStatic memberNextByte(Random)
Returns a random Byte value.
Public methodStatic memberNextByte(Random, Byte, Boolean)
Returns a random Byte value that is less or equal to the specified maxValue.
Public methodStatic memberNextByte(Random, Byte, Byte, Boolean)
Returns a random Byte value 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 memberNextDate
Returns a random DateTime that is between the specified range and has only date component.
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 random Decimal value that is less or equal to the specified maxValue.
Public methodStatic memberNextDecimal(Random, Decimal, Decimal, FloatScale)
Returns a random Decimal value that is within a specified range.
Public methodStatic memberNextDouble(Random, Double, FloatScale)
Returns a random Double value that is less or equal to the specified maxValue.
Public methodStatic memberNextDouble(Random, Double, Double, FloatScale)
Returns a random Double value that is within a specified range.
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 memberNextInt16(Random)
Returns a random Int16 value.
Public methodStatic memberNextInt16(Random, Int16, Boolean)
Returns a random Int16 value that is less or equal to the specified maxValue.
Public methodStatic memberNextInt16(Random, Int16, Int16, Boolean)
Returns a random Int16 value that is within a specified range.
Public methodStatic memberNextInt32(Random)
Returns a random Int32 value.
Public methodStatic memberNextInt32(Random, Int32, Boolean)
Returns a random Int32 value that is less or equal to the specified maxValue.
Public methodStatic memberNextInt32(Random, Int32, Int32, Boolean)
Returns a random Int32 value that is within a specified range.
Public methodStatic memberNextInt64(Random)
Returns a random Int64 value.
Public methodStatic memberNextInt64(Random, Int64, Boolean)
Returns a random Int64 value that is less or equal to the specified maxValue.
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 memberNextSByte(Random)
Returns a random SByte value.
Public methodStatic memberNextSByte(Random, SByte, Boolean)
Returns a random SByte value that is less or equal to the specified maxValue.
Public methodStatic memberNextSByte(Random, SByte, SByte, Boolean)
Returns a random SByte value 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 random Single value that is less or equal to the specified maxValue.
Public methodStatic memberNextSingle(Random, Single, Single, FloatScale)
Returns a random Single value that is within a 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, String)
Returns a random String that has the length between the specified range and consists of the specified allowedCharacters.
Public methodStatic memberNextTimeSpan
Returns a random TimeSpan that is between the specified range.
Public methodStatic memberNextUInt16(Random)
Returns a random UInt16 value.
Public methodStatic memberNextUInt16(Random, UInt16, Boolean)
Returns a random UInt16 value that is less or equal to the specified maxValue.
Public methodStatic memberNextUInt16(Random, UInt16, UInt16, Boolean)
Returns a random UInt16 value that is within a specified range.
Public methodStatic memberNextUInt32(Random)
Returns a random UInt32 value.
Public methodStatic memberNextUInt32(Random, UInt32, Boolean)
Returns a random UInt32 value that is less or equal to the specified maxValue.
Public methodStatic memberNextUInt32(Random, UInt32, UInt32, Boolean)
Returns a random UInt32 value that is within a specified range.
Public methodStatic memberNextUInt64(Random)
Returns a random UInt64 value.
Public methodStatic memberNextUInt64(Random, UInt64, Boolean)
Returns a random UInt64 value that is less or equal to the specified maxValue.
Public methodStatic memberNextUInt64(Random, UInt64, UInt64, Boolean)
Returns a random UInt64 value that is within a specified range.
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()
    {
        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