Click here to Skip to main content
15,797,925 members
Articles / Programming Languages / C#

Random Values in .NET - Some Library

Rate me:
Please Sign up or sign in to vote.
2.93/5 (20 votes)
9 May 2016CPOL1 min read 34.4K   18   43
The open source lightweight library to generate random values for string, float, date, double and decimal.


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.

Image 1

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

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

Positive float

float floatValue = Some.PositiveFloat();

will generate random value from 0 to max float.

Negative float

float floatValue = Some.NegativeFloat();

will generate random float value from min float to 0.

Float with range

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

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

Future date and time

DateTime futureDate = Some.FutureDate();

will return newer date and time when invoked.

Past date and time

DateTime pastDate = Some.PastDate();

will return older date and time when invoked.

Date with range

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)

string randomString = Some.String(20);

will generate string with 20 random characters.

Lowercase string (only letters)

string randomString = Some.StringLower(20);

will generate 20 lowercase characters as string.

Uppercase string (only letters)

string randomString = Some.StringUpper(20);

will generate 20 uppercase characters as string.

String with digits

string randomString = Some.DigitsAsString(20);

will generate 20 digits as string.


int randomInteger = Some.Integer();

will return random integer value.

Integer within range

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

will return random integer value between 100 and 456;

Positive integer

int positiveInteger = Some.PositiveInteger();

will return random positive integer value.

Negative integer

int negativeInteger = Some.NegativeInteger();

will return random negative integer value.


double randomDouble = Some.Double();

will return random double value.

Double within range

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

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

Positive double

double randomDouble = Some.PositiveDouble();

will return only positive double value.

Negative double

double randomDouble = Some.NegativeDouble();

will return only negative double value.


decimal randomDecimal = Some.Decimal();

will return random decimal value.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralMy vote of 2 Pin
Member 1069416820-Apr-16 20:42
Member 1069416820-Apr-16 20:42 
GeneralRe: My vote of 2 Pin
Pawel Sienko20-Apr-16 23:36
Pawel Sienko20-Apr-16 23:36 
GeneralRe: My vote of 2 Pin
Pawel Sienko21-Apr-16 0:08
Pawel Sienko21-Apr-16 0:08 
GeneralRe: My vote of 2 Pin
Member 1069416821-Apr-16 0:22
Member 1069416821-Apr-16 0:22 
GeneralRe: My vote of 2 Pin
Pawel Sienko21-Apr-16 0:24
Pawel Sienko21-Apr-16 0:24 
GeneralBuggy and not completed Pin
Dmitriy Gakh19-Apr-16 9:08
professionalDmitriy Gakh19-Apr-16 9:08 
GeneralRe: Buggy and not completed Pin
Pawel Sienko19-Apr-16 9:21
Pawel Sienko19-Apr-16 9:21 
GeneralRe: Buggy and not completed Pin
Dmitriy Gakh19-Apr-16 10:06
professionalDmitriy Gakh19-Apr-16 10:06 
Some tips to improve:

SomeDouble() - returns only powers of 10 ?

Double(double min, double max) - can be written without the loop.

PositiveInteger() - will never return int.MaxValue

Randomizer should support Seed value to generate similar sequences if need.

Good Luck !
GeneralRe: Buggy and not completed Pin
Pawel Sienko21-Apr-16 10:59
Pawel Sienko21-Apr-16 10:59 
GeneralConfusion Pin
Afzaal Ahmad Zeeshan19-Apr-16 7:33
professionalAfzaal Ahmad Zeeshan19-Apr-16 7:33 
GeneralRe: Confusion Pin
Pawel Sienko19-Apr-16 7:35
Pawel Sienko19-Apr-16 7:35 
GeneralMy vote of 5 Pin
Florian Rappl19-Apr-16 5:01
professionalFlorian Rappl19-Apr-16 5:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.