Answered by:
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.
Please help me.
Thanks
Sunday, March 10, 2013 7:36 AM
Answers

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".
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking：
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats Marked as answer by Arash_89 Sunday, March 10, 2013 9:59 AM
 Edited by ThankfulHeart Sunday, March 10, 2013 10:05 AM
Sunday, March 10, 2013 9:38 AM 
Sorry I've changed my codes;)
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking：
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats Marked as answer by Arash_89 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 pseudorandom 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 Arash_89 Sunday, March 10, 2013 1:59 PM
Sunday, March 10, 2013 1:58 PM 
Thanks for reply,
The instructor said to me is the uniform distribution Rand() * (ba)+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 <= xa < ba
0 <= (xa)/(ba) < 1
(xa)/(ba) = r
xa = r * (ba)
x = r * (ba) + a
 Marked as answer by Arash_89 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".
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking：
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats Marked as answer by Arash_89 Sunday, March 10, 2013 9:59 AM
 Edited by ThankfulHeart Sunday, March 10, 2013 10:05 AM
Sunday, March 10, 2013 9:38 AM 
Thank you for reply,
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;)
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking：
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats Marked as answer by Arash_89 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 programSunday, March 10, 2013 1:09 PM 
I'd use something like this
// Create a random number generator. Random rand = new Random(); // Generate a pseudorandom 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 Arash_89 Sunday, March 10, 2013 1:59 PM
Sunday, March 10, 2013 1:58 PM 
Thanks for reply,
The instructor said to me is the uniform distribution Rand() * (ba)+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 "
Ask your instructor to explain it again.
Paul Linton
Sunday, March 10, 2013 9:13 PM 
If InDoubt Then GoTo Wikipedia
 Edited by JohnWein 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 programJust use numbers[index];
index;)
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking：
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & CatsMonday, March 11, 2013 1:41 AM 
Thanks for reply,
The instructor said to me is the uniform distribution Rand() * (ba)+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 <= xa < ba
0 <= (xa)/(ba) < 1
(xa)/(ba) = r
xa = r * (ba)
x = r * (ba) + a
 Marked as answer by Arash_89 Monday, March 11, 2013 6:20 AM
Monday, March 11, 2013 2:34 AM 
Thanks for reply,
The instructor said to me is the uniform distribution Rand() * (ba)+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 <= xa < ba
0 <= (xa)/(ba) < 1
(xa)/(ba) = r
xa = r * (ba)
x = r * (ba) + a
Hi, Thanks for reply.
He told me, you should generate Random between 10 and 30 Seconds(We should use uniform distribution).
I wanted more explanation.
He wroteRand() * (ba)+a ==> and Rand() generate number between 0 and 1 .
 Edited by Arash_89 Monday, March 11, 2013 6:55 AM
Monday, March 11, 2013 6:31 AM