The first problem is because you may not have enough time to advance the seed when reinitializing your random variable. The second error comes from truncating your (what would b very large) numbers down to ints. Finally, your biggest problem is your range between your min and your max. Consider finding the range between min and max (as defined in your code) with inputs 1->20:

Doesnt Random use the time as a default seed? Why would this help?
–
LiamBMay 16 '11 at 8:39

1

@Pino, yes, but if you call this method in a loop you will probably seed the random generator with the same value. Externalizing the Random variable ensures that it is seeded only once and everytime you call .Next on it you will get a random number.
–
Darin DimitrovMay 16 '11 at 8:43

@Pino: See my answer. By not creating a new Random instance every time you allow the RNG sequence to progress and actually return its pseudo-random values. If you re-initialize (re-seed) it every time you will get the same result until the system clock advances. (E: too slow :))
–
SirViverMay 16 '11 at 8:45

2

@SirViver @Darin, but OP said that he got the same number even after he recompiled. Wouldn't that eliminate the chance that he used the same time seed?
–
amccormackMay 16 '11 at 8:55

Right, it is this overflow problem and the instance thing
–
Henk HoltermanMay 16 '11 at 10:09

Well, you have no basis to make that claim on this question, he might be doing this as a once off operation in which case it is actually a bad practice to keep a live random instance around. I can't believe you all closed this question. Have you even read it?
–
mmixMay 16 '11 at 10:21

Yes, it was a little hasty. But the dupe question comes by several times/week here. The OP should have spelled out the range thing better.
–
Henk HoltermanMay 16 '11 at 10:23

2

The way I see it the OP didn't know he has a range problem, maybe junior. IMHO, what is happening to this topic is very indicative of the blind rat-race that is going on in SO in few high volume tags such as c# and linq. It's all about points and few bother about anything else, apparently including senior members. Do you even realize that the highest ranked answer here does not even compile as it references instance field from a static method? The fact that dupe questions come along is no excuse for mistreatment of this question. Has SO jumped the shark?
–
mmixMay 16 '11 at 10:47

Random classes usually use a seed to initialize themselves, and will usually return the same sequence provided the seed is the same one :

Always reuse the same Random() instance instead of recreating one over and over again

if you want unpredictable results, use a time-dependent seed rather than an hard-coded one

It's very difficult to code a truly random number generator. Most methods use external entropy generators (such as mouse movement, cpu temperature, or even complex physical mechanisms such as helium balloons colliding one another...).

The Random instance should be created only once and then reused. The reason for this is that the RNG is by default seeded with the current system time. If you rapidly create new Random instances (and pull one value from it) then many of them will be seeded with the same timestamp, because the loop probably executes faster than the system clock advances.

Remember, a RNG initialized by seed A will always return sequence B. So if you create three Random instances all seeded with for example 123, these three instances will always return the same number on the same iteration.