본문 바로가기

VB.NET

[질문/답변] 난수발생 프로그램

반응형

안녕하세요.

VB MVP 고성만 입니다.

포럼의 난수발생 프로그램에 대해서 올려주신 질문에대한 답변입니다.

http://social.msdn.microsoft.com/Forums/ko-KR/visualbasicko/thread/940b0621-4148-4392-bb19-fd886d81c942

 

1. 난수발생시 0 이 발생할수 있다.

2. 100번만에 세가지 난수가 같은 경우가 없을수 있다.

 

소스 자체에는 크게 문제가 없습니다.

제가 조금 수정한 아래의 소스를 참고해 보세요.

반복중에 당첨숫자가 나오면 프로그램을 종료합니다.

 

 

gompang2.zip

소스파일 다운로드

 

 

 

 

 

 

 

Public Class Form1


    Private Sub onTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles onTimer.Tick
        Dim a(2) As Integer
        Dim result As Integer

        a(0) = Int(9 * Rnd() + 1)
        a(1) = Int(9 * Rnd() + 1)
        a(2) = Int(9 * Rnd() + 1)

        randomNum1.Text = a(0)
        randomNum2.Text = a(1)
        randomNum3.Text = a(2)

        If a(0) = a(1) And a(1) = a(2) And Not a(0) = 0 Then
            result = a(0)
            Call resultCall(result, True)
        End If

        Call resultCallEnd(result)

    End Sub

    Private Sub resultCall(ByVal result As Integer, ByVal judge As Boolean)

        If judge = True Then
            onSelecting.Text = ""
            onTimer.Enabled = False

            If Not result = 0 Then
                MsgBox("당첨되신 수는 : " & result & " 입니다 !! 축하합니다 ")
            Else
                MsgBox("꽝!! 다음기회를.... ")
            End If

        Else
            Progress.Value += 1

        End If

    End Sub


    Private Sub resultCallEnd(ByVal result As Integer)

        If (Progress.Value = Progress.Maximum) Then
            onSelecting.Text = ""
            onTimer.Enabled = False

            If Not result = 0 Then
                MsgBox("당첨되신 수는 : " & result & " 입니다 !! 축하합니다 ")
            Else
                MsgBox("꽝!! 다음기회를.... ")
            End If

        Else
            Progress.Value += 1

        End If

    End Sub


    Private Sub startBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startBtn.Click

        onTimer.Interval = 1
        onTimer.Enabled = True
        Progress.Value = 0
        Progress.Maximum = 1000
        onSelecting.Text = "추첨중입니다 ..."

    End Sub
End Class

 

 

 

도움되시길 바랍니다.

참고하세요.

반응형