User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 456,501 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,671 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 1495 | Replies: 5
Reply
Join Date: Sep 2007
Location: New York
Posts: 3
Reputation: phieagles76 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phieagles76's Avatar
phieagles76 phieagles76 is offline Offline
Newbie Poster

Random number generator freezing

  #1  
Sep 23rd, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Posts: 3
Reputation: arthurinvisible is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
arthurinvisible arthurinvisible is offline Offline
Newbie Poster

Re: Random number generator freezing

  #2  
Sep 24th, 2007
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
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Random number generator freezing

  #3  
Sep 24th, 2007
Get rid of those gotos and use loops! what you have so far is hideous! NO
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Sep 2007
Location: New York
Posts: 3
Reputation: phieagles76 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phieagles76's Avatar
phieagles76 phieagles76 is offline Offline
Newbie Poster

Re: Random number generator freezing

  #4  
Sep 24th, 2007
Originally Posted by iamthwee View Post
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.
Reply With Quote  
Join Date: Sep 2007
Posts: 1
Reputation: sarifin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sarifin sarifin is offline Offline
Newbie Poster

Re: Random number generator freezing

  #5  
Sep 25th, 2007
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.
Reply With Quote  
Join Date: Sep 2007
Location: New York
Posts: 3
Reputation: phieagles76 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phieagles76's Avatar
phieagles76 phieagles76 is offline Offline
Newbie Poster

Re: Random number generator freezing

  #6  
Sep 26th, 2007
Originally Posted by sarifin View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 3:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC