Skip to content

Latest commit

 

History

History
109 lines (74 loc) · 3.4 KB

File metadata and controls

109 lines (74 loc) · 3.4 KB

Exceptionless.RandomData

Build status NuGet Version Discord Donate

A utility library for generating random data in .NET. Makes generating realistic test data a breeze. Targets net8.0 and net10.0.

Getting Started

This package can be installed via the NuGet package manager. If you need help, please contact us via in-app support or open an issue. We're always here to help if you have any questions!

dotnet add package Exceptionless.RandomData

Usage

All methods are on the static RandomData class in the Exceptionless namespace.

Numbers

using Exceptionless;

int value = RandomData.GetInt(1, 100);
long big = RandomData.GetLong(0, 1_000_000);
double d = RandomData.GetDouble(0.0, 1.0);
decimal m = RandomData.GetDecimal(1, 500);

Booleans

using Exceptionless;

bool coin = RandomData.GetBool();
bool likely = RandomData.GetBool(chance: 80); // 80% chance of true

Strings

using Exceptionless;

string random = RandomData.GetString(minLength: 5, maxLength: 20);
string alpha = RandomData.GetAlphaString(10, 10);
string alphaNum = RandomData.GetAlphaNumericString(8, 16);

Words, Sentences, and Paragraphs

using Exceptionless;

string word = RandomData.GetWord();
string title = RandomData.GetTitleWords(minWords: 3, maxWords: 6);
string sentence = RandomData.GetSentence(minWords: 5, maxWords: 15);
string text = RandomData.GetParagraphs(count: 2, minSentences: 3, maxSentences: 10);
string html = RandomData.GetParagraphs(count: 2, html: true);

Dates and Times

using Exceptionless;

DateTime date = RandomData.GetDateTime();
DateTime recent = RandomData.GetDateTime(start: DateTime.UtcNow.AddDays(-30), end: DateTime.UtcNow);
DateTimeOffset dto = RandomData.GetDateTimeOffset();
TimeSpan span = RandomData.GetTimeSpan(min: TimeSpan.FromMinutes(1), max: TimeSpan.FromHours(2));

Enums

using Exceptionless;

DayOfWeek day = RandomData.GetEnum<DayOfWeek>();

Network and Versioning

using Exceptionless;

string ip = RandomData.GetIp4Address();           // e.g. "192.168.4.12"
string coord = RandomData.GetCoordinate();         // e.g. "45.123,-90.456"
string version = RandomData.GetVersion("1.0", "5.0");

Pick Random from Collection

The Random<T>() extension method picks a random element from any IEnumerable<T>:

using Exceptionless;

int[] numbers = [1, 2, 3, 4, 5];
int picked = numbers.Random();

string[] names = ["Alice", "Bob", "Charlie"];
string? name = names.Random();

Thanks to all the people who have contributed

contributors