473,508 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for loop only runs when theres a breakpoint in it

I'm trying to write a little sudoku game. When i try to set a board i
use a for loop.
Here it is
for (int c = 0; c < 6; c++)
{
Random rand = new Random();
int x = rand.Next(0, 9);
int y = rand.Next(0, 9);
int r = rand.Next(1, 9);

board[x, y] = r;

}
but for some reason it only runs once unless i put a break point
anywhere in it.
When there's a breakpoint in it it works fine though. Anyone got any
ideas to why?

Feb 16 '07 #1
3 3256
PS

"fletch" <fl*****@yahoo.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
I'm trying to write a little sudoku game. When i try to set a board i
use a for loop.
Here it is
for (int c = 0; c < 6; c++)
{
Random rand = new Random();
int x = rand.Next(0, 9);
int y = rand.Next(0, 9);
int r = rand.Next(1, 9);

board[x, y] = r;

}
but for some reason it only runs once unless i put a break point
anywhere in it.
How do you know it is only running once? What are you using as an indicator
of this? I see no counter or output that can be used to confirm that it only
runs once.

PS
When there's a breakpoint in it it works fine though. Anyone got any
ideas to why?

Feb 16 '07 #2
Hi Fletch,

I'm betting the problem is Random rand = new Random(); It takes the
randomized seed from a timestamp, but if you use the same seed later you
will get the same sequence of numbers. Since you initialize rand at each
turn the timestamp will not have time to change, effectively causing rand
to be seeded with the same number at each turn. You probably see only one
cell get a number since x, y and r will get the same each time. When you
use a breakpoint, you give the timestamp plenty of time to change, giving
you a new seed the next turn.

Move the initialization of rand outside the loop to fix this.

Random rand = new Random();
for (int c = 0; c < 6; c++)
{
int x = rand.Next(0, 9);
int y = rand.Next(0, 9);
int r = rand.Next(1, 9);

board[x, y] = r;
}
On Fri, 16 Feb 2007 06:48:22 +0100, fletch <fl*****@yahoo.comwrote:
I'm trying to write a little sudoku game. When i try to set a board i
use a for loop.
Here it is
for (int c = 0; c < 6; c++)
{
Random rand = new Random();
int x = rand.Next(0, 9);
int y = rand.Next(0, 9);
int r = rand.Next(1, 9);

board[x, y] = r;

}
but for some reason it only runs once unless i put a break point
anywhere in it.
When there's a breakpoint in it it works fine though. Anyone got any
ideas to why?


--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 16 '07 #3
fletch <fl*****@yahoo.comwrote:
I'm trying to write a little sudoku game. When i try to set a board i
use a for loop.
Here it is
for (int c = 0; c < 6; c++)
{
Random rand = new Random();
int x = rand.Next(0, 9);
int y = rand.Next(0, 9);
int r = rand.Next(1, 9);

board[x, y] = r;

}
but for some reason it only runs once unless i put a break point
anywhere in it.
When there's a breakpoint in it it works fine though. Anyone got any
ideas to why?
It's running several times, but you're getting the same entries each
time.

See http://pobox.com/~skeet/csharp/miscu...ticrandom.html
for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
18651
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
8
2014
by: Hazz | last post by:
What is the triggering action that raises the event which causes my breakpoint to be reached, repeatedly, but does not allow my form to ever remain visible. I set a breakpoint on the line...
34
1255
by: jcrouse | last post by:
Here is my code: Dim sr1 As StreamReader = New StreamReader(Application.StartupPath & "\Controls.ini") strGameExists = "no" intCheck = 0 Do
6
9898
by: Gary Wessle | last post by:
Hi using the debugger, I happen to be on a line inside a loop, after looping few times with "n" and wanting to get out of the loop to the next line, I set a break point on a line after the loop...
1
349
by: fletch | last post by:
I'm trying to write a little sudoku game. When i try to set a board i use a for loop. Here it is for (int c = 0; c < 6; c++) { Random rand = new Random(); int x = rand.Next(0, 9); int y =...
5
1554
by: john009 | last post by:
hmmmm...thanks to all that are willing to help i am using VB 6...theres more than one question i wish to ask please bare with me...i have to read in information from a file which the program...
1
1171
by: darrel | last post by:
Hi there i have a problem with the looping statement that i must use in order to achieve wha i want.. Its like this i have a time scheduling system that compares records in a database the compare...
4
1809
vikas251074
by: vikas251074 | last post by:
Loop runs indefinite times. suppose v_router = 5 is entered by user, then following code runs indefinite times i.e. record is inserted indefinite times though each have different i and v_ip_address....
3
2864
Claus Mygind
by: Claus Mygind | last post by:
For some reason my ajax request keeps repeating. It appears to be one of these places where the code runs ahead of itself, because when I put in a breakpoint in firebug, I can step through the data....
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.