Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
Print
(untagged)

Random Values in .NET - Some Library

0.00/5 (No votes)
9 May 2016 1  
The open source lightweight library to generate random values for string, float, date, double and decimal.

Introduction

I figured out that there is no library in .NET Framework which allows to generate random values of predefined types. I decided to create Some library which generates most random values of predefined types like decimal, float, string, etc. with many options of generating.

This is to generate random values in UnitTests and database values.

How To Get It?

You can download the source code from GitHub or nuget by typing Some keyword. See the screenshot below.

How To Use It?

When you get the newest version, now you can random values of predefined types. Let's see some examples.

Default seed value

C#
// <summary>
/// Default constructor
/// </summary>
static Some()
{
        RandomCharacters = new StringBuilder();
        randomizer = new Random((int)DateTime.Now.Ticks);
}

Positive float

C#
float floatValue = Some.PositiveFloat();

will generate random value from 0 to max float.

Negative float

C#
float floatValue = Some.NegativeFloat();

will generate random float value from min float to 0.

Float with range

C#
float floatValue = Some.Float(99.1f, 341.21f);

will generate float random value between 99.1f and 341.21f.

Future date and time

C#
DateTime futureDate = Some.FutureDate();

will return newer date and time when invoked.

Past date and time

C#
DateTime pastDate = Some.PastDate();

will return older date and time when invoked.

Date with range

C#
DateTime date = Some.Date(DateTime.Now, DateTime.Now.AddDays(10))

will generate date withing specified range in input parameters of invoking method.

String (only letters)

C#
string randomString = Some.String(20);

will generate string with 20 random characters.

Lowercase string (only letters)

C#
string randomString = Some.StringLower(20);

will generate 20 lowercase characters as string.

Uppercase string (only letters)

C#
string randomString = Some.StringUpper(20);

will generate 20 uppercase characters as string.

String with digits

C#
string randomString = Some.DigitsAsString(20);

will generate 20 digits as string.

Integer

C#
int randomInteger = Some.Integer();

will return random integer value.

Integer within range

C#
int randomInteger = Some.Integer(100, 456);

will return random integer value between 100 and 456;

Positive integer

C#
int positiveInteger = Some.PositiveInteger();

will return random positive integer value.

Negative integer

C#
int negativeInteger = Some.NegativeInteger();

will return random negative integer value.

Double

C#
double randomDouble = Some.Double();

will return random double value.

Double within range

C#
double randomDouble = Some.Double(1.765d, 100.091892d);

will return random double value between 1.765d and 100.091892d.

Positive double

C#
double randomDouble = Some.PositiveDouble();

will return only positive double value.

Negative double

C#
double randomDouble = Some.NegativeDouble();

will return only negative double value.

Decimal

C#
decimal randomDecimal = Some.Decimal();

will return random decimal value.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here