I know Excel has a built in Random function, but I wanted to try this with using that as a little challenge.
Basically I wanted to create something that would simulate the lottery, so that I could press a button and have it draw 6 random numbers from 1 to 49.
The code I used is as follows (it's not commented but I explain it below)
So a prerequisite for this to work, is that cell A1 on whichever sheet needs to be formatted to display the time including milliseconds. All this does really is take the milliseconds from the time of iteration and then convert them into a number between 1 and 49.
This is repeated for each ball with a check to ensure that number hasn't been drawn already. If the number has been drawn already, the ball is redrawn.
This version of the code will loop 300 times to get a wide spread of results though you may want to turn that number up or down depending on how long you want to wait.
So:
getRandomTwoDigits: This pulls the milliseconds from the current time. 1 is added to the number drawn (so that we don't draw the number 0) If the starting number is 98 or 99 the number is redrawn. (inefficient but the easiest way to do it)
pullNumber: This just checks to see if the number pulled from the ms is above 49, if it is then we minus 49 to ensure there's an even chance of any number being drawn.
addNumber: This adds the number drawn to a results table
checkNumber: Checks the current row of the results table to see if the number has been drawn during this game. If it has, the ball is redrawn
Problems
One big problem is that the code takes about the same amount of time to run. Which means every time a 3 is drawn as the first number, the second number will always be an 11 and the third an 18. (At least on my machine)
The other big problem is that some numbers are just never drawn. This one puzzles me as I can't work out why. But the number 1 is never drawn, nor the number 2 and a few others somewhere in the middle never show up either.
Any idea of where I'm going wrong?
Basically I wanted to create something that would simulate the lottery, so that I could press a button and have it draw 6 random numbers from 1 to 49.
The code I used is as follows (it's not commented but I explain it below)
Code:
Private ballNumber As Integer
Private rowTracker As Integer
Private columnTracker As Integer
Private devTracker As Integer
Private moveOn As Boolean
---------------------------------
Sub playLotteryLots()
Application.ScreenUpdating = False
rowTracker = 2
For j = 1 To 300
Call playLottery
Next j
MsgBox ("Done")
Application.ScreenUpdating = True
End Sub
-----------------------------------
Sub playLottery()
columnTracker = 1
For columnTracker = 1 To 6
moveOn = False
While moveOn = False
Call getRandomTwoDigits
Call pullNumber
moveOn = True
If columnTracker <> 1 Then checkNumber
Wend
Call addNumber
Call updateDoneList
Next columnTracker
rowTracker = rowTracker + 1
End Sub
----------------------------------
Private Sub getRandomTwoDigits()
Dim validNumber As Boolean
validNumber = False
While validNumber = False
Range("A1").Formula = "=NOW()"
ballNumber = Right(Range("A1").Value, 2)
If ballNumber < 98 Then
ballNumber = ballNumber + 1
validNumber = True
End If
Wend
End Sub
------------------------------------------------
Private Sub pullNumber()
If ballNumber > 49 Then
ballNumber = ballNumber - 49
End If
End Sub
------------------------------------------------
Private Sub addNumber()
Cells(rowTracker, (columnTracker + 3)).Value = ballNumber
End Sub
------------------------------------------------
Private Sub checkNumber()
For i = 2 To 6
If ballNumber = Cells(rowTracker, i).Value Then
moveOn = False
End If
Next i
End Sub
So a prerequisite for this to work, is that cell A1 on whichever sheet needs to be formatted to display the time including milliseconds. All this does really is take the milliseconds from the time of iteration and then convert them into a number between 1 and 49.
This is repeated for each ball with a check to ensure that number hasn't been drawn already. If the number has been drawn already, the ball is redrawn.
This version of the code will loop 300 times to get a wide spread of results though you may want to turn that number up or down depending on how long you want to wait.
So:
getRandomTwoDigits: This pulls the milliseconds from the current time. 1 is added to the number drawn (so that we don't draw the number 0) If the starting number is 98 or 99 the number is redrawn. (inefficient but the easiest way to do it)
pullNumber: This just checks to see if the number pulled from the ms is above 49, if it is then we minus 49 to ensure there's an even chance of any number being drawn.
addNumber: This adds the number drawn to a results table
checkNumber: Checks the current row of the results table to see if the number has been drawn during this game. If it has, the ball is redrawn
Problems
One big problem is that the code takes about the same amount of time to run. Which means every time a 3 is drawn as the first number, the second number will always be an 11 and the third an 18. (At least on my machine)
The other big problem is that some numbers are just never drawn. This one puzzles me as I can't work out why. But the number 1 is never drawn, nor the number 2 and a few others somewhere in the middle never show up either.
Any idea of where I'm going wrong?