Skip to content

Extensions to the Random class to generate numbers, strings, and random picks

License

Notifications You must be signed in to change notification settings

NightOwl888/RandomizedTesting

Randomized Testing

Nuget Azure DevOps builds (branch) GitHub GitHub Sponsors

This is a C# port of the only the generators from the Java randomizedtesting library.

In addition to the random generators, the RandomExtensions class contains several useful low-level extension methods for generating numbers, text (html-like, regex-like, unicode, realistic unicode, plain ASCII), random picks from collections, or random regular expression instances.

Why would you want tests to have random data? This is to fill situations where it is not practical to test the entire range of values in a single test run, for example, with applications that analyze text. Tests can instead be designed to provide different input every time they run to catch edge cases that are difficult to test for. Generating random data also has many other purposes, such as quickly generating a set of text files to benchmark with without having to store several hundred MB worth of files (since the same random seed will always generate the same data).

Random Generator Examples

// Create a System.Random instance
Random random = new Random();

// Create a random bool
bool boolValue = random.NextBoolean();

// Create a random long
long longValue = random.NextInt64();

// Create a random long between 10 (inclusive) and 100 (inclusive)
long longRangeValue = random.NextInt64(minValue: 10, maxValue: 100);

// Create a random float
float floatValue = random.NextSingle();

// Create a random BigInteger (from System.Numerics)
BigInteger bigIntValue = random.NextBigInteger();

// Pick a random KeyValuePair from a generic dictionary
var dictionary = new Dictionary<string, string>
{
    ["one"] = "oneValue",
    ["two"] = "twoValue",
    ["three"] = "threeValue",
    ["four"] = "fourValue",
    ["five"] = "fiveValue",
};
KeyValuePair<string, string> kvp = random.NextFrom(dictionary);

// Pick a random element from an array
var array = new int[] { 1, 4, 7, 10, 14, 74, 136 };
var arrayElement = random.RandomFrom(array);

// Create a random string (plain ASCII)
string asciiValue = random.NextSimpleString(maxLength: 15);

// Create a random string with specific chars
string specValue = random.NextSimpleStringRange(minChar: 'A', maxChar: 'F', maxLength: 10);

// Create a random Unicode string (may contain surrogate pairs)
string unicodeValue = random.NextUnicodeString(maxLength: 20);

// Create a random realistic Unicode string (random characters picked from the same Unicode block)
string realUnicodeValue = random.NextRealisticUnicodeString(maxLength: 30);

// Fill part of a char[] with random Unicode characters
char[] chars = "The quick brown fox jumped over the lazy dog.".ToCharArray();
random.NextFixedLengthUnicodeString(chars, startIndex: 8, length: 20);

// Generate a random regex-like string
string regexLike = random.NextRegexishString(maxLength: 20);

// Generate a random HTML-like string
string htmlLike = random.NextHtmlishString(numElements: 144);

// Randomly recase a string
string toRecase = "This Is A Pascal Cased String";
string recased = random.NextStringRecasing(toRecase);

// Generate a random Regex that compiles
Regex pattern = random.NextRegex();

Building and Testing

To build the project from source, see the Building and Testing documentation.

Saying Thanks

If you find this library to be useful, please star us on GitHub and consider a financial sponsorship so we can continue bringing you great free tools like this one.

GitHub Sponsors

About

Extensions to the Random class to generate numbers, strings, and random picks

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages