Click here to Skip to main content
15,670,482 members
Articles / Programming Languages / C#
Tip/Trick
Posted 19 Apr 2016

Tagged as

Stats

32.5K views
18 bookmarked

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
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.

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

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, 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

 
QuestionApplication ? Pin
Dmitriy Gakh6-May-16 12:31
professionalDmitriy Gakh6-May-16 12:31 
QuestionWhy not using extension methods... Pin
Philippe Mori6-May-16 11:06
Philippe Mori6-May-16 11:06 
AnswerRe: Why not using extension methods... Pin
Pawel Sienko6-May-16 11:13
Pawel Sienko6-May-16 11:13 
GeneralRe: Why not using extension methods... Pin
Philippe Mori6-May-16 11:20
Philippe Mori6-May-16 11:20 
GeneralRe: Why not using extension methods... Pin
Philippe Mori6-May-16 13:24
Philippe Mori6-May-16 13:24 
QuestionWhy you haven't used generic ? Pin
delfo30-Apr-16 10:16
delfo30-Apr-16 10:16 
AnswerRe: Why you haven't used generic ? Pin
Pawel Sienko3-May-16 21:30
Pawel Sienko3-May-16 21:30 
AnswerRe: Why you haven't used generic ? Pin
Philippe Mori6-May-16 11:09
Philippe Mori6-May-16 11:09 
PraiseGood work! Pin
VR Karthikeyan26-Apr-16 6:20
professionalVR Karthikeyan26-Apr-16 6:20 
GeneralRe: Good work! Pin
Pawel Sienko26-Apr-16 6:26
Pawel Sienko26-Apr-16 6:26 
QuestionFound bug? Just let me know. Pin
Pawel Sienko21-Apr-16 10:00
Pawel Sienko21-Apr-16 10:00 
AnswerRe: Found bug? Just let me know. Pin
sx200824-Apr-16 8:07
sx200824-Apr-16 8:07 
GeneralRe: Found bug? Just let me know. Pin
Pawel Sienko24-Apr-16 8:24
Pawel Sienko24-Apr-16 8:24 
GeneralRe: Found bug? Just let me know. Pin
Dmitriy Gakh25-Apr-16 8:34
professionalDmitriy Gakh25-Apr-16 8:34 
GeneralRe: Found bug? Just let me know. Pin
Pawel Sienko25-Apr-16 9:30
Pawel Sienko25-Apr-16 9:30 
GeneralRe: Found bug? Just let me know. Pin
Philippe Mori6-May-16 6:55
Philippe Mori6-May-16 6:55 
GeneralRe: Found bug? Just let me know. Pin
Pawel Sienko6-May-16 7:00
Pawel Sienko6-May-16 7:00 
GeneralRe: Found bug? Just let me know. Pin
Philippe Mori6-May-16 7:26
Philippe Mori6-May-16 7:26 
GeneralRe: Found bug? Just let me know. Pin
Pawel Sienko6-May-16 7:35
Pawel Sienko6-May-16 7:35 
GeneralRe: Found bug? Just let me know. Pin
Philippe Mori6-May-16 7:58
Philippe Mori6-May-16 7:58 
I am the third person in this single thread that tell you that it should not be a static class and explain why.

I even show you an example of how improved code would also to use more than one generator.

If you don't understand why, at least follow suggestions of experts. And take some software design courses and you will then understand why your code is bad. Even my improved version is still very basic as I cannot chage which generator is used internally if I want another kind of distribution...

You should read about SOLID (object-oriented design) - Wikipedia, the free encyclopedia[^].
Philippe Mori

GeneralRe: Found bug? Just let me know. Pin
Pawel Sienko6-May-16 8:21
Pawel Sienko6-May-16 8:21 
GeneralRe: Found bug? Just let me know. Pin
Philippe Mori6-May-16 9:49
Philippe Mori6-May-16 9:49 
GeneralRe: Found bug? Just let me know. Pin
Pawel Sienko6-May-16 10:01
Pawel Sienko6-May-16 10:01 
GeneralRe: Found bug? Just let me know. Pin
Philippe Mori6-May-16 10:55
Philippe Mori6-May-16 10:55 
GeneralRe: Found bug? Just let me know. Pin
Pawel Sienko6-May-16 11:03
Pawel Sienko6-May-16 11:03 

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.