Option Explicit
Private PickedNumbers() As Integer
'change these two constants to modify the allowed range of numbers
Private Const SMALLEST_NUMBER As Integer = 1
Private Const BIGGEST_NUMBER As Integer = 75
Private Sub cmdNextNumber_Click()
On Error GoTo ErrHandler
Dim tmpNumber As Integer
Dim a As Integer
Dim textToShow As String
Randomize Timer
If (UBound(PickedNumbers)) > (BIGGEST_NUMBER - SMALLEST_NUMBER) Then
'end of game, all numbers have been picked already
'the reset button must be pressed to continue
textToShow = "--"
cmdNextNumber.Enabled = False
Else
If UBound(PickedNumbers) = 0 Then
'call a function to generate a pseud-random number
tmpNumber = generateANumber
Else
'check if the generated number has already been chosen by looping through the stored number array
Do
tmpNumber = generateANumber
For a = 0 To UBound(PickedNumbers)
If tmpNumber = PickedNumbers(a) Then Exit For
DoEvents
Next a
If a - 1 = UBound(PickedNumbers) Then Exit Do 'all stored numbers searched, new number is indeed new
DoEvents
Loop
End If
'store the new number in the array, expand the array for the next number, and update the total numbers display
PickedNumbers(UBound(PickedNumbers)) = tmpNumber
textToShow = tmpNumber
ReDim Preserve PickedNumbers(UBound(PickedNumbers) + 1)
lblTotalNumberOfNumbers.Caption = UBound(PickedNumbers)
End If
'set the text on the ball
ActivePresentation.Slides(1).Shapes(2).TextFrame.TextRange.Text = textToShow
Exit Sub
ErrHandler:
Select Case Err.Number
Case 9
ReDim PickedNumbers(0)
Resume
End Select
End Sub
Private Function generateANumber() As Integer
Dim tmpNumber As Integer
Do Until tmpNumber >= SMALLEST_NUMBER And tmpNumber <= BIGGEST_NUMBER
'generate a pseudo-random number using the Rnd function
tmpNumber = Round(Rnd * BIGGEST_NUMBER)
DoEvents
Loop
generateANumber = tmpNumber
End Function
Private Sub cmdReset_Click()
Erase PickedNumbers
cmdNextNumber.Enabled = True
lblTotalNumberOfNumbers.Caption = ""
ActivePresentation.Slides(1).Shapes(2).TextFrame.TextRange.Text = ""
End Sub
Private Sub txtBallLabel_Change()
ActivePresentation.Slides(1).Shapes(3).TextFrame.TextRange.Text = txtBallLabel.Text
End Sub
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (5)
Commented:
Just a thought when most numbers have "gone" it could take a while to generate a number that is not in the array of picked numbers.
I would maybe
1. generate an array with ALL the numbers
2. Randomly choose a position in the array and read the number stored there.
3. Read the top number in the array and make the chosen position this value
4. Redim 1 to Ubound-1 to strip the old top number
Chose another random number based on the new Ubound
Hope that makes sense.
Author
Commented:Commented:
Commented:
Commented: