RandEx 1.0.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package RandEx --version 1.0.7
                    
NuGet\Install-Package RandEx -Version 1.0.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RandEx" Version="1.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RandEx" Version="1.0.7" />
                    
Directory.Packages.props
<PackageReference Include="RandEx" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RandEx --version 1.0.7
                    
#r "nuget: RandEx, 1.0.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RandEx@1.0.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RandEx&version=1.0.7
                    
Install as a Cake Addin
#tool nuget:?package=RandEx&version=1.0.7
                    
Install as a Cake Tool

RandEx

RandEx is a library designed to provide advanced randomisation functions in C#. It provides various methods for generating random numbers, selecting random elements, shuffling collections and more.

Features

  • Custom seed management: Initialize random number generation with custom seeds.
  • Flexible random number generation:
    • Random integers (with various range options).
    • Random doubles (uniform distribution).
    • Random booleans.
    • Gaussian distribution: Generate random numbers according to a normal distribution.
  • Advanced Utility Methods:
    • Generate random date/time objects.
    • Select random items from collections.
    • Shuffle collections.
    • Generate random colours, GUIDs, and byte arrays.
    • Enumeration support: Easily select random values from any enumeration.
  • Thread-safe implementation: Each thread maintains its own state for safe and efficient concurrent use, making it suitable for multi-threaded environments.

Installation

Run dotnet add package RandEx

Example

// Import the namespace
using RandEx;

Basic Random Number Generation

// Random integer between two values
int randomInt = RandomEx.GetRandomInt(10, 50);

// Random double between 0.0 and 1.0
double randomDouble = RandomEx.GetRandomDouble();

// Random boolean
bool randomBool = RandomEx.GetRandomBool();

Deterministic Randomization with Custom Seeds

// Set a custom seed for deterministic random number generation
RandomEx.SetSeed(123456);
int deterministicRandom = RandomEx.GetRandomInt();

Gaussian Distribution

// Generate a random number with a Gaussian (normal) distribution
double randomGaussian = RandomEx.GetRandomGaussian();

Random Collection Utilities

var list = new List<int> { 1, 2, 3, 4, 5 };

// Get a random element
int randomElement = RandomEx.GetRandomElement(list);

// Shuffle the list
RandomEx.Shuffle(list);

Random DateTime

// Random DateTime between two dates
DateTime randomDate = RandomEx.GetRandomDateTime(
    new DateTime(2000, 1, 1),
    new DateTime(2020, 1, 1)
);

Random Bytes and Colors

// Generate a random byte array
byte[] randomBytes = RandomEx.GetRandomBytes(16);

// Generate a random color
var (R, G, B) = RandomEx.GetRandomByteColor();

Random Enumeration Values

enum Colors { Red, Green, Blue, Yellow }

// Get a random enum value
Colors randomColor = RandomEx.GetRandomEnumValue<Colors>();

Random Char

// Generate a char (From 'a' to 'z')
char randomChar = RandomEx.GetRandomChar('a', 'z');
// Generate a char (From '0' to '9')
char randomChar2 = RandomEx.GetRandomChar('0', '9');
// Generate a char (From ' ' to '~') default
char randomChar3 = RandomEx.GetRandomChar();

API Reference

Random Number Methods
Method Description
SetSeed(int seed) Sets a custom seed for deterministic randomization.
GetRandomInt(int minValue = 0, int maxValue = int.MaxValue) Generates a random integer between minValue and maxValue.
GetRandomDouble() Generates a random double between 0.0 and 1.0.
GetRandomBool() Generates a random boolean (true or false).
GetRandomGaussian() Generates a random number from a Gaussian distribution (mean=0, stddev=1).
Collection Utilities
Method Description
GetRandomElement<T>(IList<T> collection) Returns a random element from the given collection.
Shuffle<T>(List<T> values) Shuffles the elements in the provided list.
GetRandomEnumValue<T>() Returns a random value from the specified enumeration type. (e.g., Enum Colors { Red, Green }, it selects one randomly).
Specialized Randomization
Method Description
GetRandomDateTime(DateTime? minDate = null, DateTime? maxDate = null) Generates a random DateTime between the specified minDate and maxDate. If minDate is null, it defaults to DateTime.MinValue. Similarly, if maxDate is null, it defaults to DateTime.MaxValue.
GetRandomByteColor() Generates a random color represented as a byte tuple (R, G, B).
GetRandomGuid() Generates a random GUID.
GetRandomBytes(int length) Generates a random byte array of the specified length.
GetRandomByte() Generates a random byte (0-255).
GetRandomChar(char minChar = ' ', char maxChar = '~') Generates a random character from a specified inclusive character range, based on Unicode values
GetRandomString(int length, RandomStringOptions options = ...) Generates a random string of the specified length using selected character sets.By default, it includes lowercase letters, uppercase letters, and numbers.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.10 124 12/9/2024
1.0.9 104 12/9/2024
1.0.8 116 12/4/2024
1.0.7 123 12/1/2024
1.0.5 109 11/30/2024
1.0.4 104 11/30/2024
1.0.3 113 11/27/2024
1.0.2 107 11/27/2024
1.0.1 92 11/27/2024
1.0.0 120 11/27/2024