반응형

* VBNET 랜덤(Random) 클래스를 이용한 간단한 로또(Lotto) 숫자 생성 예제...

 

 

- 사용한 컨트롤 : Button 1개, Label 1개, TextBox 1개

 

전체 소스 코드

Form1.vb

 

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = Create_LottoNumber()
    End Sub

    Private Function Create_LottoNumber() As String

        Dim strTmp As String = ""
        Dim strNumber As String = ""
        Dim strBonusNumber As String = ""
        Dim imsiNum(7) As Integer

        For i As Integer = 0 To 6
            Dim rnd As Random = New Random()
            Dim bCheck As Boolean = False

            imsiNum(i) = rnd.Next(1, 45)

            For j As Integer = 0 To i - 1
                '같은 번호 이면... 다시
                If imsiNum(i) = imsiNum(j) Then
                    i = i - 1
                    bCheck = True
                End If
            Next

            '문자열로 합치기
            If Not bCheck Then strTmp += imsiNum(i).ToString() + ", "

        Next

        'Lotto Number 만 가려내기
        strNumber = strTmp.Substring(0, strTmp.LastIndexOf(",") - 3)
        '마지막 , 제거
        strNumber = strNumber.Substring(0, strNumber.LastIndexOf(","))

        '보너스 번호 가져 오기
        strBonusNumber = strTmp.Substring(strTmp.LastIndexOf(",") - 5, 2).Trim()
        strBonusNumber = strBonusNumber.Substring(0, strBonusNumber.LastIndexOf(","))

        Return "LottoNumber : " + strNumber + "  BonusNumber : " + strBonusNumber
    End Function

End Class

 

 

* 예제 결과

 

반응형

+ Recent posts