Click here to Skip to main content
15,881,838 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
im trying to make thing,that generates random number, not includes previos(1-10)
VB
Public Class Form1
    Dim NewTicketNumber As Integer
    Dim IsTicketDone1 As Boolean = False
    Dim IsTicketDone2 As Boolean = False
    Dim IsTicketDone3 As Boolean = False
    Dim IsTicketDone4 As Boolean = False
    Dim IsTicketDone5 As Boolean = False
    Dim IsTicketDone6 As Boolean = False
    Dim IsTicketDone7 As Boolean = False
    Dim IsTicketDone8 As Boolean = False
    Dim IsTicketDone9 As Boolean = False
    Dim IsTicketDone10 As Boolean = False
    Private Sub Make()
        NewTicketNumber = GetRandom(1, 10)
        If NewTicketNumber = 1 Then
            If IsTicketDone1 = True Then
                Make()
            Else
                IsTicketDone1 = True
            End If
        ElseIf NewTicketNumber = 2 Then
            If IsTicketDone2 = True Then
                Make()
            Else
                IsTicketDone2 = True
            End If
        ElseIf NewTicketNumber = 3 Then
            If IsTicketDone3 = True Then
                Make()
            Else
                IsTicketDone3 = True
            End If
        ElseIf NewTicketNumber = 4 Then
            If IsTicketDone4 = True Then
                Make()
            Else
                IsTicketDone4 = True
            End If
        ElseIf NewTicketNumber = 5 Then
            If IsTicketDone5 = True Then
                Make()
            Else
                IsTicketDone5 = True
            End If
        ElseIf NewTicketNumber = 6 Then
            If IsTicketDone6 = True Then
                Make()
            Else
                IsTicketDone6 = True
            End If
        ElseIf NewTicketNumber Then
            If IsTicketDone7 = True Then
                Make()
            Else
                IsTicketDone7 = True
            End If
        ElseIf NewTicketNumber Then
            If IsTicketDone8 = True Then
                Make()
            Else
                IsTicketDone8 = True
            End If
        ElseIf NewTicketNumber Then
            If IsTicketDone9 = True Then
                Make()
            Else
                IsTicketDone9 = True
            End If
        ElseIf NewTicketNumber Then
            If IsTicketDone10 = True Then
                Make()
            Else
                IsTicketDone10 = True
            End If
        End If
        Label1.Text = NewTicketNumber
        If IsTicketDone1 = True And IsTicketDone2 = True And IsTicketDone3 = True And IsTicketDone4 = True And IsTicketDone5 = True And IsTicketDone6 = True And IsTicketDone7 = True And IsTicketDone8 = True And IsTicketDone9 = True And IsTicketDone10 = True Then
            MsgBox("All Done!")
        End If
    End Sub
    Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
        Dim Generator As System.Random = New System.Random()
        Return Generator.Next(Min, Max)
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Make()
    End Sub
End Class
Posted
Updated 12-May-13 6:43am
v2

Unfortunately, this code does not deserve debugging, because this is not how a software developer writes code. A software developer would never repeat identical declaration several times like your 10 Boolean variables. A software developer would declare an array or use some collection. A software developer would never write "if something = True then …" because "something" is already Boolean, so it should be "if something then …"; and a software developer will never try to write that long "if" block and will use a loop.

Most importantly, a software developer would not wait until anyone teaches how to do it but would derive right code from pure logic and first principles.

I don't know how to effectively help in this case. If I fix the bug, it won't really help.

—SA
 
Share this answer
 
First, yes, bad programming...

However getting to the point, a random number is just that, random. It is not guaranteed to give you any value, only a value within that range. So lets say you run this and it NEVER randomly gives you the value of 2, your code will continue to loop forever until it gets that value. Maybe it will give it, maybe it won't, but leaving it up to chance that it will makes for interesting and hard to find bugs, as you are finding out.

* ALSO *

You stopped giving equality checks after NewTicketNumber = 6 (so 7,8,9, and 10 do not check for a value). On top of that you are calling Make() again before setting the flag to true, which means that at some point you will get stack overflow exceptions. Set the flag first, then call Make() again.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900