Chapter149:GeneratingRandomNumbers in C#

No Comments

Parameters                                                                         Details

Seed Avalueforgeneratingrandomnumbers.Ifnotset,thedefaultvalueisdeterminedbythecurrent system time.

minValue         Generatednumbers won’tbe smaller thanthis value. Ifnot set, thedefault valueis 0.

maxValue       Generated numbers will be smaller than this value. If not set, the default value is Int32.MaxValue. return value Returns a number with random value.

Section149.1:Generatearandomint

Thisexamplegeneratesrandomvaluesbetween0and2147483647.

Randomrnd=newRandom();

intrandomNumber=rnd.Next();

Section149.2:Generatearandomintinagivenrange

GeneratearandomnumberbetweenminValueandmaxValue-1.

Randomrnd=newRandom();

varrandomBetween10And20=rnd.Next(10,20);

Section149.3:Generatingthesamesequenceofrandom numbers over and over again

WhencreatingRandominstanceswiththesameseed,thesamenumberswillbegenerated.

intseed=5;

for(inti=0;i<2;i++)

{

Console.WriteLine(“Randominstance”+i);

Randomrnd=newRandom(seed);

for(intj=0;j<5;j++)

{

Console.Write(rnd.Next());

Console.Write(“”);

}

Console.WriteLine();

}

Output:

Random instance 0

726643700 610783965 564707973 1342984399 995276750

Random instance 1

726643700 610783965 564707973 1342984399 995276750

Section149.4:Createmultiplerandomclasswithdifferent seedssimultaneously

TwoRandomclasscreatedatthesametimewillhavethesameseedvalue.

UsingSystem.Guid.NewGuid().GetHashCode()cangetadifferentseedeveninthesametime.

Randomrnd1=newRandom();

Randomrnd2=newRandom();

Console.WriteLine(“First5randomnumberinrnd1”);

for(inti=0;i<5;i++)

Console.WriteLine(rnd1.Next());

Console.WriteLine(“First5randomnumberinrnd2”);

for(inti=0;i<5;i++)

Console.WriteLine(rnd2.Next());

rnd1=newRandom(Guid.NewGuid().GetHashCode());

rnd2=newRandom(Guid.NewGuid().GetHashCode());

Console.WriteLine(“First5randomnumberinrnd1usingGuid”);

for(inti=0;i<5;i++)

Console.WriteLine(rnd1.Next());

Console.WriteLine(“First5randomnumberinrnd2usingGuid”);

for(inti=0;i<5;i++)

Console.WriteLine(rnd2.Next());

AnotherwaytoachievedifferentseedsistouseanotherRandominstancetoretrievetheseedvalues.

RandomrndSeeds=newRandom();

Randomrnd1=newRandom(rndSeeds.Next());

Randomrnd2=newRandom(rndSeeds.Next());

ThisalsomakesitpossibletocontroltheresultofalltheRandominstancesbysettingonlytheseedvaluefor the

rndSeeds.Alltheotherinstanceswillbedeterministicallyderivedfromthatsingleseedvalue.

Section149.5:GenerateaRandomdouble

Generatea random numberbetween 0 and1.0. (not including1.0)

Randomrnd=newRandom();

varrandomDouble=rnd.NextDouble();

Section149.6:Generatearandomcharacter

GeneratearandomletterbetweenaandzbyusingtheNext()overloadforagivenrangeofnumbers,then converting the resulting intto a char

Randomrnd=newRandom();

charrandomChar=(char)rnd.Next(‘a’,’z’);

//’a’and‘z’areinterpretedasintsforparametersforNext()

Section149.7:Generateanumberthatisapercentageofa max value

A common need for random numbers it to generate a number that is X%of some max value. this can be done by treating the result of NextDouble()as a percentage:

varrnd=newRandom();

varmaxValue=5000;

varpercentage=rnd.NextDouble();

varresult=maxValue*percentage;

//supposeNextDouble()returns.65,resultwillhold65%of5000:3250.

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts
No Comments

Leave a Comment