Question

Is rand.Next(randSeed.Next(1,10), randSeed2.Next(10, 20)) more random than rand.Next(1,20)?

I'm always getting the same number from the random class when i run code every 10 minutes (using a timer from the system.timers.timer class)

Was it helpful?

Solution

A seed is used to initialize a random number generator, like so:

Random rand = new Random(1234);  // 1234 is the seed.

Or to make it different each time your program is run, you can do this:

Random rand = new Random((int) DateTime.Now.Ticks);  // Use current time for seed.

or simply:

Random rand = new Random();  // If you don't specify a seed, it uses the current time by default.

Note: If you use a fixed seed number, then you will get the same sequences of output from Rand() each time after initialization.


Only after the seed initialization do you start to use the random number generator normally (such as calling Next()). In other words, seeds are not supposed to be used as parameters of Next().

If, however, you are trying to make the seed itself more 'random' by using another random number generator to produce it, then don't bother. Mathematically, this won't be any more random. You are better off using a cryptographic random number generator instead, such as RNGCryptoServiceProvider.

Note: Although cryptographic random number generators are more 'random', they are also slower.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top