How to generate random numbers, with the info
Posted on August 26, 2011
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 | 'Coding Basics - How to generate random numbers, with the info '1) How many values - txtValue '2) What the max is - txtMax '3) What the min is - txtMin '4) If they want duplicate random numbers, or not ' - OptionBoxes ' - Option1 = Double ' - Option2 = No Double Option Explicit Dim HowMany As Integer Dim RndNum As Integer Dim Answer As String Dim I As Integer Dim Search As Integer Private Sub cmdGenerate_Click() On Error GoTo Problem Randomize ReDim Ans(TxtValue) If Option1.Value = True And TxtMax - TxtMin < TxtValue Then MsgBox "It is impossible to generate that amount of random numbers without doubles." Exit Sub End If Answer = "" For HowMany = 1 To TxtValue 5 RndNum = Int(Rnd * TxtMax) + TxtMin If RndNum > TxtMax Then GoTo 5 If Option2.Value = True Then Ans(HowMany) = RndNum Else For Search = 1 To HowMany If RndNum = Ans(Search) Then GoTo 5 Next Search Ans(HowMany) = RndNum End If Next HowMany For I = 1 To TxtValue Answer = Answer & Ans(I) If I <> TxtValue Then Answer = Answer & ", " Next I MsgBox Answer Exit Sub Problem: MsgBox Err.Description End Sub |