You must create instances of the Random instance before the loop.
public static void rand() { Random r = new Random(); for (int j = 0; j < 10; j++) { Console.WriteLine(r.Next(100)); } }
And here is the explanation:
... The initial default value is derived from the system clock and has a final resolution. As a result, various Random objects that are created in close sequence when the default constructor is called will have the same initial default values ββand, therefore, will create the same sets of random numbers. This problem can be avoided by using one random object to generate all random numbers. ...
If you want to use different instances of Random , you must use different seed values. For example, the variable j :
public static void rand() { for(int j = 0; j < 10; j++) { Random r = new Random(j); Console.WriteLine(r.Next(100)); } }
Answering your question: ... if I always need to check if the debug mode gives me the correct results.
No, you do not need to doubt the results of debug mode. They are right. Your understanding of them may be wrong.
Dmytro
source share