tags:

views:

206

answers:

9

I use this code to generate a random number.

 Random R = new Random(0);
 int Rand = R.Next(7);

but i get the same random number in each run of program.

A: 

Use time as initial seed of your PRNG.

osgx
+6  A: 

you have to change the seed of your random number generator object everytime you run your program, as what i've seen from you example, your current seed is 0, so you have to change it to something else if you want to get a different stream of random number... just a thought!

ultrajohn
A: 

You need to seed the Random class with something more variable than 0. I normally use DataTime.Now.Ticks or you could use a new Guid's interger value.

Jonesie
A: 

You need to seed the random generator. You can use as follows:

Random R = new Random(DateTime.Now.Millisecond);

int Rand = R.Next(7);

logicnp
+3  A: 

Seed your (pseudo)-random generator using a non-constant value, e.g. current time and date:

Random R = new Random(DateTime.Now.Ticks);

Read more about pseudo-random generators at Wikipedia.

vladr
+1 for unchecked - that's a new one on me.
Paddy
See the `Examples` section in the docs @ http://msdn.microsoft.com/en-us/library/ctssatww.aspx
vladr
not needed at all, the parameterless constructor takes time based seed value automatically
Marek
+8  A: 

Hi Mosa

Remove the 0 from the constructor and you'll get different random numbers.

If you pass a number to the constructor it's used as seed, by always specifying 0 you'll always get the same sequence.

You can specify an int32 which is random, but the easiest is to just not pass any parameter and you get a timebased seed

Per Jakobsen
+1 for being the only one to suggest the obvious; to use the parameterless constructor.
Guffa
A: 

Random number generators generate a new 'random' value based on the previous number generated. The seed is the initial value for this.

Seeding with the same value (like 0 in your example code) basically tells the random number generator to start with the same number each time. Having the exact same random number generated each time means your code becomes restartable. Example: Simulations use this to restart the simulation with changed parameters, but with the same 'data set'.

Another example:

I want to send myself a motivational message each day. Sometimes the messages are garbled. Being able to rerun the script, producing the same message again and again during a day, makes fixing this simple. In Perl code this means:

# By initialising the random generator with the day number since
# the epoch, we get the same quote during one day.
srand(time()/(24*3600));
my $i = int(rand(@messages));

If you want to produce different numbers each time, you will have to set this seed to something random. The options are many, like time, PID, delay between two keystrokes by the user, some value derived from the ethernet interface, etc. or more likely a combination of the above like time*PID.

Hope this clarifies the idea behind the concept of a random number seed value.

Coroos
A: 

if we want a random number between 1 and 100 the code would look like this: RandomNumberGenerator.GetRandomInt(1, 100)

Joby Kurian
A: 

The most secure way to generate random number is to use the System.Security.Cryptography.RandomNumberGenerator class.

Here is an example that will generate a number between 1 and 100;

public Number Rand()
{

    byte[] Salt = new byte[8];

    System.Security.Cryptography.RandomNumberGenerator.Create().GetBytes(Salt);

    decimal result = 0;

    foreach (byte b in Salt)
{

    result = result * 255 + b;

}

while (result > 100)

{

    result /= 10;

}

return result

}

Noam