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.
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.
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.
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...
MSDN explicitly addresses this "problem" in the remarks section of the MSDN docs for the Random class, including a sample!