Home > How-To Library > Coding Basics
How to generate random numbers, with the info
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** '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
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.