Help with random numbers
-
When I run the following code I get the same number 36 times, but when I step through it in debug I get 'random' numbers?!?! Any idea why?
for (int a = 0; a < 36; a++) { Random generator = new Random(); int randomValue; randomValue = generator.Next(1, 20); list3.Add(a, randomValue); }
David Wilkes
-
When I run the following code I get the same number 36 times, but when I step through it in debug I get 'random' numbers?!?! Any idea why?
for (int a = 0; a < 36; a++) { Random generator = new Random(); int randomValue; randomValue = generator.Next(1, 20); list3.Add(a, randomValue); }
David Wilkes
-
Sorry, "list3" is a custom data type similar to a listbox.
David Wilkes
-
Sorry, "list3" is a custom data type similar to a listbox.
David Wilkes
Hi, you create a new random generator every time; they each start with a dfeault seed value, which depends on the current time. As a result, while single stepping it seems all right; when running it finishes in less than 1 second, so all seeds are the same. You should create and use only one generator outside the loop and keep using it. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, you create a new random generator every time; they each start with a dfeault seed value, which depends on the current time. As a result, while single stepping it seems all right; when running it finishes in less than 1 second, so all seeds are the same. You should create and use only one generator outside the loop and keep using it. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, you create a new random generator every time; they each start with a dfeault seed value, which depends on the current time. As a result, while single stepping it seems all right; when running it finishes in less than 1 second, so all seeds are the same. You should create and use only one generator outside the loop and keep using it. :)
Luc Pattyn [My Articles] [Forum Guidelines]
That makes total sense. Figured it was something simple. Thanks!!!
David Wilkes
-
When I run the following code I get the same number 36 times, but when I step through it in debug I get 'random' numbers?!?! Any idea why?
for (int a = 0; a < 36; a++) { Random generator = new Random(); int randomValue; randomValue = generator.Next(1, 20); list3.Add(a, randomValue); }
David Wilkes
Random generator = new Random(); for (int a = 0; a < 36; a++) { int randomValue; randomValue = generator.Next(1, 20); list3.Add(a, randomValue); }
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]