Produce a Random Number in a Range Using C#

Produce a random number in a range using C#

You can try

//for integers
Random r = new Random();
int rInt = r.Next(0, 100);

//for doubles
int range = 100;
double rDouble = r.NextDouble()* range;

Have a look at

Random Class, Random.Next Method (Int32, Int32) and Random.NextDouble Method

How to generate random numbers from a range in c#

You need this overload of Random.Next():

public virtual int Next(
int minValue,
int maxValue
)

Where:

minValue = The inclusive lower bound of the random number returned.

maxValue = The exclusive upper bound of the random number returned.
maxValue must be greater than or equal to minValue.

Note the words inclusive and exclusive in the parameter descriptions. This means the minimum value can be returned in the possible values, while the maximum value will not be possible.

This is further clarified in the description of the return value:

Return Value - A 32-bit signed integer greater than or equal to
minValue and less than maxValue; that is, the range of return values
includes minValue but not maxValue. If minValue equals maxValue,
minValue is returned.

For example, to get single digit values between 0 and 9 (inclusive), you'd use:

int value = rnd.Next(0, 10); // return a value between 0 and 9 inclusive

To get double digits, you'd use:

int value = rnd.Next(10, 100); // return a value between 10 and 99 inclusive

Finally, to get triple digit numbers, you'd use:

int value = rnd.Next(100, 1000); // return a value between 100 and 999 inclusive

Generate Random Number in range with greater likelihood in the middle

This is probably a naïve approach but you could split the random generation into multiple steps.

For example, say you want 70% of the ages to be between 20 and 70, you could introduce an additional random check:

if (random.NextDouble() >= 0.3)
{
return random.Next(20, 70); // generate ages 20, 21, ..., 68, 69
}

Now, we're left with the other 30%. I'd assume that there will be more young people than old people, so I'll attribute 20% to young people, and 10% to old people. We can update the code above to handle this:

double percentage = random.NextDouble();

if (percentage >= 0.3) // 1.0 - 0.3 = 70%
{
return random.Next(20, 70);
}
else if (percentage >= 0.1) // 0.3 - 0.1 = 20%
{
return random.Next(0, 20);
}
else // remaining 10%
{
return random.Next(70, 100);
}

Try it online - in the sample I bucket ages into the nearest 5 and then provide the count for the distribution.

Loop that generates 10 random number in the range 20-50 and displays it

You want:

    int min=20,max=50;
Random RandomNumber = new Random();

for (var count = 0; count<10; count++)
Console.WriteLine(RandomNumber.Next(min, max));

The for loop runs 10 iterations, and each iteration Console.WriteLine()'s a new random number between min and max.

Random number generator with different numbers

One way to solve it would be to simulate what a real lotto machine does.

First, put the 49 balls in the bucket:

var bucket = Enumerable.Range(1, 49).ToList();

Then in a loop, determine a random index in the current bucket, get the number at this index and remove it so that it cannot be drawn again

var random = new Random();
for (var i = 0; i < 6; i++)
{
var index = random.Next(bucket.Count);
var number = bucket[index];
Console.WriteLine(number);
bucket.RemoveAt(index);
}

Generate random number from 2 different ranges

int num = rnd.Next(1, 3)==1 ? rnd.Next(10, 21) : rnd.Next(50,61);

or

int num = rnd.Next(10, 32);
if (num>20) num+=29;

or just for fun (don't use with large ranges, and runs slow anyhow):

var ranges=new []{ Tuple.Create(10,20), Tuple.Create(50,60)};
var rnd=new Random();
var possible=ranges.Select(x=>Enumerable.Range(x.Item1,x.Item2-x.Item1+1))
.SelectMany(x=>x)
.Distinct();
var num=possible.Skip(rnd.Next(0,possible.Count())).First();


Related Topics



Leave a reply



Submit