Re: Hi

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Hans Kesting (news.2.hansdk_at_spamgourmet.com)
Date: 09/27/04


Date: Mon, 27 Sep 2004 13:13:33 +0200

Mahendran wrote:
> Hi,
>
> I need to use Random numbers with in Maximum Limit.
>
> I have used Random Class from .Net Package.
>
> Random R =new Random();
>
> int I;
>
> for(I = 0; I < 10; I++)
>
> {
>
> MessageBox.Show(R.Next(I).ToString());
>
> }
>
>
>
> But it gives me a duplicate random number. So if any Other Class or
> Source Code for generate Random numbers in the loop variable please
> suggest me.
>
>
> Thanks for your help
>
>
>
> Mahendran

What exactly do you want? First you mention "maximum", then you have
a problem with a "duplicate" number...

For "maximum":
the Next() method has several overloads. Some of them specify a range
for the result (usually: including the lower limit, excluding the upper limit).
An upper limit of 0 seems strange ...

For "duplicate":
The random number is *random*, there should be no (apparent) relation
between the number returned now and numbers returned previously.
This doesn't mean "no identical numbers", it means "no check for duplication".
Duplicate numbers /can/ happen. What do you want to happen when you
try to read 7 distinct integer values in the range 1 to 6?

Hans Kesting