user285127
user285127

Reputation:

Create a random int number that differ from previous run of program

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.

Upvotes: 7

Views: 1911

Answers (10)

Jonesie
Jonesie

Reputation: 7285

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 integer value.

Upvotes: 0

Choco Smith
Choco Smith

Reputation: 1668

Complete Code:

 public static class RandomHelper
{
    static object _myLock = new object();
    static Random _random = new Random();

    public static int RandomNumber(int min, int max)
    {

        lock (_myLock)
        {
            if (min == max)
                return min;

            if (min > max)
                return _random.Next(max, min);

            return _random.Next(min, max);
        }

}

Upvotes: 0

Noam
Noam

Reputation: 5286

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

}

Upvotes: 0

Joby Kurian
Joby Kurian

Reputation: 3927

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

Upvotes: 0

Coroos
Coroos

Reputation: 390

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.

Upvotes: 0

vladr
vladr

Reputation: 66681

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.

Upvotes: 3

Per Jakobsen
Per Jakobsen

Reputation: 3777

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

Upvotes: 13

logicnp
logicnp

Reputation: 5836

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

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

int Rand = R.Next(7);

Upvotes: 0

ultrajohn
ultrajohn

Reputation: 2597

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!

Upvotes: 10

osgx
osgx

Reputation: 94235

Use time as initial seed of your PRNG.

Upvotes: 0

Related Questions