-1

i use

Random rnd = new Random();
x=rnd.Next(10);

but every time i get the same number. how to fix it and get different numbers? Tell me easy method.

thanks. sorry for bad english.

Saska
  • 1,021
  • 6
  • 19
  • 32
  • possible duplicate of [Random number generator only generating one random number](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) –  Mar 30 '14 at 08:38

4 Answers4

13

Random's default constructor uses the current time as its seed. Therefore, if you initialize multiple Random objects in rapid succession (such as in a loop, for instance), they will share the same seed.

Create your Random object once and use it multiple time, or create a seed beforehand and use it to initialize your generators.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • 2
    In the vast majority of cases creating a static `Random` instance and then using it as long as the program lives, is the proper way to go. If you need more then you likely (hopefully) know enough about PRNGs anyway to avoid the common mistakes. – Joey Jun 20 '10 at 16:43
  • 1
    Create a single Random object and then use it multiple times instead of creating a new one each time. Also, System.Random should **NOT** be used for anything security related. For that you should use RNGCryptoServiceProvider (http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx) – Eadwacer Jun 20 '10 at 17:09
4

MSDN explicitly addresses this "problem" in the remarks section of the MSDN docs for the Random class, including a sample!

http://msdn.microsoft.com/en-us/library/system.random.aspx

balpha
  • 50,022
  • 18
  • 110
  • 131
Lucero
  • 59,176
  • 9
  • 122
  • 152
  • The amazing thing about this is that it would have been faster to implement a proper PRNG with good seeding compared to documenting its flaws. – CodesInChaos Mar 30 '14 at 11:37
0

Obligatory XKCD reference:

http://xkcd.com/221/

Community
  • 1
  • 1
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • 4
    *sigh* ... Even if it still were funny the twentieth time you have to see this crop up in connection to (pseudo-)random numbers ... it's not an answer to the question, nor particularly helpful. – Joey Jun 20 '10 at 16:39
  • also, http://www.dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/00000/2000/300/2318/2318.strip.gif is much more relevant – balpha Jun 20 '10 at 17:17
  • 1
    That's the other one that appears frequently, albeit slightly less so. Still, both are unlikely to help anyone and serve only to entertain a select few. – Joey Jun 20 '10 at 17:20
  • Yeahh...it's getting pretty old. Let it die. – mpen Jun 20 '10 at 17:23
  • @Johannes I agree with you for the most part, although the Dilbert one actually has some sense to it (while the XKCD one isn't much more than a joke). Either way, neither of them is an answer to the question. – balpha Jun 20 '10 at 17:29
-2

Make sure you use the constructor only once. You can add a seed.

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

Then you can make calls to as you actually did it.

x=rnd.Next(10);

But make sure you don't have the constructor and the call to the Next() method inside a loop or something similar...

Jonathan
  • 11,809
  • 5
  • 57
  • 91
  • 5
    That's quite a poor seed. You only get 1000 different sequences out of it, one of which starts with 0 (which is a *very* poor seed for most PRNGs [excluding the WELL generators, but even then]). The default for `Random` is the current number of ticks which is much better than this but still you can get the same sequence if you happen to create new instances too fast. The real problem is likely that they create a new `Random` instance over and over again in a loop. – Joey Jun 20 '10 at 16:42
  • @Jonathan I think you confused `DateTime.Millisecond` (which is always between 0 and 999) and thus extremely bad with `TimeSpan.TotalMilliseconds`. The latter is about as good as the default seed (milliseconds since boot) – CodesInChaos Mar 30 '14 at 11:36
  • It's still a bad seed, since a ms is a long time in computer terms (and getting "longer" as computers continue to become faster). Depending on the job at hand, for instance if an external process does this and this process is invoked in fast succession, it may reuse a seed. – Lucero Mar 30 '14 at 17:11
  • @Lucero It's impossible to generate a good 31 bit seed without persisting a counter. If it's a random seed the rate of collisions is too high (Already happens after 50k seeds or so). If its the time in millisecond it changes too rarely, if it's a finer time it'll overflow too quickly. In short, if you care about quality, `System.Random` isn't for you. – CodesInChaos Apr 02 '14 at 15:35