# Random numbers with Uniform Distribution • ### Question

• Hi,

I want generate Random numbers between 10 until 30 with uniform distribution in visual C#.

I want use the rand method.

Thanks

Sunday, March 10, 2013 7:36 AM

• List<int> numbers = new List<int>();

Random r = new Random(Guid.NewGuid().GetHashCode());

for(int i=10;i<=30;++i)

{

numbers.Insert(r.Next(0,numbers.Count),i);

}

Then you can take number from "numbers".

Help by clicking：
Click to Donate
Click to feed Dogs & Cats

• Marked as answer by Sunday, March 10, 2013 9:59 AM
• Edited by Sunday, March 10, 2013 10:05 AM
Sunday, March 10, 2013 9:38 AM
• Sorry I've changed my codes;)

Help by clicking：
Click to Donate
Click to feed Dogs & Cats

• Marked as answer by Sunday, March 10, 2013 1:08 PM
Sunday, March 10, 2013 10:04 AM
• I'd use something like this

```// Create a random number generator.
Random rand = new Random();

// Generate a pseudo-random integer with uniform distribution like this:
// The first argument is the INCLUSIVE lower bound
// The second argument is the EXCLUSIVE upper bound
int x = rand.Next(10,30);

Console.WriteLine( x );
```

To me, uniform distribution just means that all the answers are equally likely.  Do I misunderstand your original question?

• Marked as answer by Sunday, March 10, 2013 1:59 PM
Sunday, March 10, 2013 1:58 PM
• The instructor said to me is the uniform distribution Rand() * (b-a)+a
But i don't understand .

We should generate uniformly distributed random numbers between 10 and 30

It depends if the upper bound is inclusive or exclusive and whether you are dealing with integers or floats.

for floating point numbers:

The Random.NextDouble function will never return exactly 1.0  It returns a number than is greater than or equal to 0.0, but strictly less than 1.0.

So in your case, to generate numbers where 10.0 <= x < 30.0, you can use the following:

double x = random.NextDouble() * (30.0 - 10.0) + 10.0;

This works because random.NextDouble is a number where

0.0 <= r < 1.0

so

0.0 <= r * 20.0 < 20.0

and

10.0 <= r * 20.0 + 10.0 < 30.0

You can derive what the instructor told you like this by trying to find an expression with the normal distribution with the range 0 <= r < 1  (where r is the random number)

a <= x < b

0 <= x-a < b-a

0 <= (x-a)/(b-a) < 1

(x-a)/(b-a) = r

x-a = r * (b-a)

x = r * (b-a) + a

• Marked as answer by Monday, March 11, 2013 6:20 AM
Monday, March 11, 2013 2:34 AM

### All replies

• List<int> numbers = new List<int>();

Random r = new Random(Guid.NewGuid().GetHashCode());

for(int i=10;i<=30;++i)

{

numbers.Insert(r.Next(0,numbers.Count),i);

}

Then you can take number from "numbers".

Help by clicking：
Click to Donate
Click to feed Dogs & Cats

• Marked as answer by Sunday, March 10, 2013 9:59 AM
• Edited by Sunday, March 10, 2013 10:05 AM
Sunday, March 10, 2013 9:38 AM
• The above code with uniformly distributed random numbers are created?

I have an error,

numbers.Insert(i, r.Next(0, numbers.Count));

Argument out of range exeption was handled.

Sunday, March 10, 2013 10:01 AM
• Sorry I've changed my codes;)

Help by clicking：
Click to Donate
Click to feed Dogs & Cats

• Marked as answer by Sunday, March 10, 2013 1:08 PM
Sunday, March 10, 2013 10:04 AM
• Hi
How can I see the output of the random number?
I want to use the program
Sunday, March 10, 2013 1:09 PM
• I'd use something like this

```// Create a random number generator.
Random rand = new Random();

// Generate a pseudo-random integer with uniform distribution like this:
// The first argument is the INCLUSIVE lower bound
// The second argument is the EXCLUSIVE upper bound
int x = rand.Next(10,30);

Console.WriteLine( x );
```

To me, uniform distribution just means that all the answers are equally likely.  Do I misunderstand your original question?

• Marked as answer by Sunday, March 10, 2013 1:59 PM
Sunday, March 10, 2013 1:58 PM
• The instructor said to me is the uniform distribution Rand() * (b-a)+a
But i don't understand .

We should generate uniformly distributed random numbers between 10 and 30

Sunday, March 10, 2013 2:04 PM
• "The instructor said to me "

"But i don't understand "

Paul Linton

Sunday, March 10, 2013 9:13 PM
• If InDoubt Then GoTo Wikipedia
• Edited by Sunday, March 10, 2013 9:30 PM
Sunday, March 10, 2013 9:27 PM
• Hi
How can I see the output of the random number?
I want to use the program

Just use numbers[index];

index;)

Help by clicking：
Click to Donate
Click to feed Dogs & Cats

Monday, March 11, 2013 1:41 AM
• The instructor said to me is the uniform distribution Rand() * (b-a)+a
But i don't understand .

We should generate uniformly distributed random numbers between 10 and 30

It depends if the upper bound is inclusive or exclusive and whether you are dealing with integers or floats.

for floating point numbers:

The Random.NextDouble function will never return exactly 1.0  It returns a number than is greater than or equal to 0.0, but strictly less than 1.0.

So in your case, to generate numbers where 10.0 <= x < 30.0, you can use the following:

double x = random.NextDouble() * (30.0 - 10.0) + 10.0;

This works because random.NextDouble is a number where

0.0 <= r < 1.0

so

0.0 <= r * 20.0 < 20.0

and

10.0 <= r * 20.0 + 10.0 < 30.0

You can derive what the instructor told you like this by trying to find an expression with the normal distribution with the range 0 <= r < 1  (where r is the random number)

a <= x < b

0 <= x-a < b-a

0 <= (x-a)/(b-a) < 1

(x-a)/(b-a) = r

x-a = r * (b-a)

x = r * (b-a) + a

• Marked as answer by Monday, March 11, 2013 6:20 AM
Monday, March 11, 2013 2:34 AM
• The instructor said to me is the uniform distribution Rand() * (b-a)+a
But i don't understand .

We should generate uniformly distributed random numbers between 10 and 30

It depends if the upper bound is inclusive or exclusive and whether you are dealing with integers or floats.

for floating point numbers:

The Random.NextDouble function will never return exactly 1.0  It returns a number than is greater than or equal to 0.0, but strictly less than 1.0.

So in your case, to generate numbers where 10.0 <= x < 30.0, you can use the following:

double x = random.NextDouble() * (30.0 - 10.0) + 10.0;

This works because random.NextDouble is a number where

0.0 <= r < 1.0

so

0.0 <= r * 20.0 < 20.0

and

10.0 <= r * 20.0 + 10.0 < 30.0

You can derive what the instructor told you like this by trying to find an expression with the normal distribution with the range 0 <= r < 1  (where r is the random number)

a <= x < b

0 <= x-a < b-a

0 <= (x-a)/(b-a) < 1

(x-a)/(b-a) = r

x-a = r * (b-a)

x = r * (b-a) + a