Create a random int number that is different from the previous program run

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 every run of the program.

+7
c # random
source share
10 answers

you need to change the seed of your random number generator every time you run your program, like what I saw from the example, your current seed is 0, so you need to change it to something else if you want to get a different random stream numbers ... just a thought!

+10
source share

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

If you pass the number to the constructor that he used as the seed, always specifying 0, you will always get the same sequence.

You can specify int32, which is random, but the simplest is just not to pass any parameter, and you get time-based seed

+13
source share

Place your (pseudo) random generator using a fickle value, for example. current time and date:

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

Read more about pseudo-random generators on Wikipedia .

+3
source share

Use time as the seed of your PRNG.

0
source share

You need to align the Random class with something more variable than 0. I usually use DataTime.Now.Ticks or you can use the new value of integer Guid.

0
source share

You need to plant a random generator. You can use the following:

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

int Rand = R.Next (7);

0
source share

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

Interacting with the same value (for example, 0 in your code example) basically tells the random number generator to start with the same number each time. Having the same random number generated each time means your code is getting restarted. Example: Simulation uses this to restart a simulation with changed parameters, but with the same β€œdata set”.

Another example:

I want to send myself a motivational message every day. Sometimes messages are distorted. The ability to restart the script by creating the same message again and again throughout the day makes 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 create different numbers every time, you will need to set this seed to something random. The parameters are many, for example, time, PID, delay between two keystrokes by the user, some value received from the ethernet interface, etc., Or rather, the combination is higher, like time*PID .

We hope that this clarifies the idea of ​​the concept of the value of a random number.

0
source share

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

0
source share

The safest way to generate a random number is to use the System.Security.Cryptography.RandomNumberGenerator class.

Here is an example that will generate a number from 1 to 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 

}

0
source share

The 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); } } 
0
source share

All Articles