Hello,
I'm working on a sudoku type game with 4x4 squares rather than the typical 9x9 and I have written code to generate a random number for each button when new game is clicked the problem is it will generate 1 to 4 boards and then it freezes up I'm sure itis getting stuck in the loop but I just don't understand why it's freezing. Here is the code I wrote:

``````Private Sub newgme_clk(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click

Dim randomnum(15) As Integer
Dim i As Integer

Randomize()
randomnum(0) = CInt(Int(Rnd() * 4 + 1))

restartnumber2:
For i = 1 To 4

Randomize()
randomnum(1) = CInt(Int(Rnd() * 4 + 1))
If randomnum(1) = randomnum(0) Then
GoTo restartnumber2
End If
Next

restartnumber3:
For i = 1 To 4

Randomize()
randomnum(2) = CInt(Int(Rnd() * 4 + 1))
If randomnum(2) = randomnum(0) Or randomnum(2) = randomnum(1) Then
GoTo restartnumber3
End If
Next

restartnumber4:
For i = 1 To 4

Randomize()
randomnum(3) = CInt(Int(Rnd() * 4 + 1))
If randomnum(3) = randomnum(0) Or randomnum(3) = randomnum(1) Or randomnum(3) = randomnum(2) Then
GoTo restartnumber4
End If
Next

restartnumber5:
For i = 1 To 4

Randomize()
randomnum(4) = CInt(Int(Rnd() * 4 + 1))
If randomnum(4) = randomnum(0) Or randomnum(4) = randomnum(1) Then
GoTo restartnumber5
End If
Next

restartnumber6:
For i = 1 To 4

Randomize()
randomnum(5) = CInt(Int(Rnd() * 4 + 1))
If randomnum(5) = randomnum(0) Or randomnum(5) = randomnum(1) Or randomnum(5) = randomnum(4) Then
GoTo restartnumber6
End If
Next

restartnumber7:
For i = 1 To 4

Randomize()
randomnum(6) = CInt(Int(Rnd() * 4 + 1))
If randomnum(6) = randomnum(2) Or randomnum(6) = randomnum(3) Or randomnum(6) = randomnum(4) Or randomnum(6) = randomnum(5) Then
GoTo restartnumber7
End If
Next

restartnumber8:
For i = 1 To 4

Randomize()
randomnum(7) = CInt(Int(Rnd() * 4 + 1))
If randomnum(7) = randomnum(2) Or randomnum(7) = randomnum(3) Or randomnum(7) = randomnum(4) Or randomnum(7) = randomnum(5) Or randomnum(7) = randomnum(6) Then
GoTo restartnumber8
End If
Next

restartnumber9:
For i = 1 To 4

Randomize()
randomnum(8) = CInt(Int(Rnd() * 4 + 1))
If randomnum(8) = randomnum(0) Or randomnum(8) = randomnum(4) Then
GoTo restartnumber9
End If
Next

restartnumber10:
For i = 1 To 4

Randomize()
randomnum(9) = CInt(Int(Rnd() * 4 + 1))
If randomnum(9) = randomnum(1) Or randomnum(9) = randomnum(5) Or randomnum(9) = randomnum(8) Then
GoTo restartnumber10
End If
Next

restartnumber11:
For i = 1 To 4
Rnd(-1)
Randomize()
randomnum(10) = CInt(Int(Rnd() * 4 + 1))
If randomnum(10) = randomnum(2) Or randomnum(10) = randomnum(6) Or randomnum(10) = randomnum(8) Or randomnum(10) = randomnum(9) Then
GoTo restartnumber11
End If
Next

restartnumber12:
For i = 1 To 4
Rnd(-1)
Randomize()
randomnum(11) = CInt(Int(Rnd() * 4 + 1))
If randomnum(11) = randomnum(3) Or randomnum(11) = randomnum(7) Or randomnum(11) = randomnum(8) Or randomnum(11) = randomnum(9) Or randomnum(11) = randomnum(10) Then
GoTo restartnumber12
End If
Next

restartnumber13:
For i = 1 To 4
Rnd(-1)
Randomize()
randomnum(12) = CInt(Int(Rnd() * 4 + 1))
If randomnum(12) = randomnum(0) Or randomnum(12) = randomnum(4) Or randomnum(12) = randomnum(8) Or randomnum(12) = randomnum(9) Then
GoTo restartnumber13
End If
Next

restartnumber14:
For i = 1 To 4
Rnd(-1)
Randomize()
randomnum(13) = CInt(Int(Rnd() * 4 + 1))
If randomnum(13) = randomnum(1) Or randomnum(13) = randomnum(5) Or randomnum(13) = randomnum(9) Or randomnum(13) = randomnum(8) Or randomnum(13) = randomnum(12) Then
GoTo restartnumber14
End If
Next

restartnumber15:
For i = 1 To 4
Rnd(-1)
Randomize()
randomnum(14) = CInt(Int(Rnd() * 4 + 1))
If randomnum(14) = randomnum(2) Or randomnum(14) = randomnum(6) Or randomnum(14) = randomnum(10) Or randomnum(14) = randomnum(11) Or randomnum(14) = randomnum(12) Or randomnum(14) = randomnum(13) Then
GoTo restartnumber15
End If
Next

restartnumber16:
For i = 1 To 4
Rnd(-1)
Randomize()
randomnum(15) = CInt(Int(Rnd() * 4 + 1))
If randomnum(15) = randomnum(3) Or randomnum(15) = randomnum(7) Or randomnum(15) = randomnum(11) Or randomnum(15) = randomnum(12) Or randomnum(15) = randomnum(13) Or randomnum(15) = randomnum(14) Then
GoTo restartnumber16
End If
Next

Button1.Text = randomnum(0)
Button2.Text = randomnum(1)
Button3.Text = randomnum(2)
Button4.Text = randomnum(3)
Button5.Text = randomnum(4)
Button6.Text = randomnum(5)
Button7.Text = randomnum(6)
Button8.Text = randomnum(7)
Button9.Text = randomnum(8)
Button10.Text = randomnum(9)
Button11.Text = randomnum(10)
Button12.Text = randomnum(11)
Button13.Text = randomnum(12)
Button14.Text = randomnum(13)
Button15.Text = randomnum(14)
Button16.Text = randomnum(15)

End Sub
End Class``````

I have tried changing the randomnum(15) to a long and a byte as well as a single data
type and it hasn't made a difference. the other difference that I noticed was that by adding Rnd(-1) before the randomize() on the last few loops helped but it's still freezing up. Thanks for any help.

4
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by phieagles76

it seems to me that you are right it is getting stuck in a loop probably on random number six or seven, because your random number is between one and five and when you get to those loops the numbers are all taken up and it just keeps repeating the loop so that number <> to any previous random numbers

Get rid of those gotos and use loops! what you have so far is hideous! NO

Get rid of those gotos and use loops! what you have so far is hideous! NO

It would be great if you could elaborate for me please. I'm slightly confused on what your saying. I'm new to VB and I was under the impression that I was using a loop.

Hello:

I was just going through your problem.

Are you able to solve your freezing problem? If not, i will look further towards the solution.

Thanks.

Hello:

I was just going through your problem.

Are you able to solve your freezing problem? If not, i will look further towards the solution.

Thanks.

No I still haven't been able to figure it out thanks for your assistance.I really appreciate it I really just want to know why it doesn't work it seems like it should.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.