Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB VB.NET Help random
im trying to make thing,that generates random number, not includes previos(1-10)
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 12-May-13 7:42am
Edited 12-May-13 7:43am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v3

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

  Print Answers RSS
0 Maciej Los 315
1 OriginalGriff 273
2 Aajmot Sk 224
3 Marcin Kozub 210
4 Richard MacCutchan 200
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 12 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100