How to generate Random Lotto Numbers
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 'Math - How to generate Random Lotto Numbers Option Explicit 'Open a new project with a command button and a texbox with index property set to 0 Public Function LottoNumbers(ByVal NumbersToGen As Integer, _ ByVal HighestNumber As Integer) As String Dim nCount As Long ReDim narr(0 To NumbersToGen) As Integer Dim strResult As String Dim bMatch As Boolean Dim intTemp As Integer Dim intSubCounter As Integer strResult = "" If NumbersToGen > HighestNumber Then MsgBox "Numbers To Generate is larger than Highest Number" Else Randomize For nCount = 0 To NumbersToGen - 1 'loop through number of numbers to generate intTemp = Int(Rnd() * HighestNumber) + 1 'assign a temporary random integer For intSubCounter = 0 To NumbersToGen - 1 'loop through array and check for duplicates If narr(intSubCounter) = intTemp Then 'if a match was found then... bMatch = True 'set flag nCount = nCount - 1 'and decrement counter so as to get a new random integer Exit For 'if a match then exit the inner loop End If Next intSubCounter If bMatch = False Then 'if integer was unique narr(nCount) = intTemp 'assign the integer into array End If bMatch = False 'reset flag to false each time Debug.Print narr(nCount) Next nCount For nCount = 0 To NumbersToGen - 1 'load the final array into string strResult = narr(nCount) & "," & strResult Next End If Erase narr LottoNumbers = strResult 'return array results in function End Function Private Sub Command1_Click() Dim sTemp() As String Dim x As Long sTemp = Split(LottoNumbers(6, 49), ",", , vbTextCompare) For x = 0 To UBound(sTemp) - 1 Load Text1(Text1.Count) Text1(x).Visible = True Text1(x).Top = Text1(x).Top + x * 400 Text1(x).Text = sTemp(x) Next Me.Height = Text1(Text1.ubound - 1).Top + 1600 End Sub Private Sub Form_Load() Text1(0).Move 100, 100, 1000, 385 Command1.Move 1500, 100, 1000, 385 End Sub |