473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 3252
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
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
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
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
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
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
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
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
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.