'Generates five random numbers between 1 and 20
'with no duplicates. (concept stolen from Tom Ogilvy)
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
Sub GetThem()
Dim arrCheck(1 To 20) As Long
Dim arrList(1 To 5) As Long
Dim j As Long
Dim N As Long
Const LNG_PLUG As Long = 999
j = 1
Do While j < 6
'Get a random number
Randomize
N = Int(Rnd * 20 + 1)
'If number unique then add to arrList.
If arrCheck(N) <> LNG_PLUG Then
arrList(j) = N
arrCheck(N) = LNG_PLUG
j = j + 1
End If
Loop
Range("B5:F5").Value = arrList()
End Sub
'--------------
"msburza" wrote in message
I'm trying to write a code that will generate random numbers and no
number appears more than once.
Code:
--------------------
For I = 0 To 3
For J = 0 To I
Do
n = Int(4 * Rnd) + 1 'random number generated
Array1(I) = n 'random number settled in an array
If (I = I - J) Then 'checking for the same place in array
numsOK = True
Else
If Array1(I) = Array1(I - J) Then 'comparing two different places in an array
numsOK = False
Else
numsOK = True
End If
End If
Loop Until numsOK = True
Next J 'only the last number and the first one always differ, other numbers repeat
--------------------
Next I Plz help me and tell why the program isnt working. I have no
idea, how to make it work.
--
msburza
Bookmarks