-
I need to create a specific number of random numbers within a number range.
For example, I need 15 random numbers out of the number range of 1 through 100. These 15 numbers can not be duplicates.
Since the number range will always start with one, I wouldn't need to pass that number.
So I am looking for a function like this:
getrandomnumbers(intAmount, intRange)
then have the random numbers put in a variable array
Anyone have some code that I could adapt to my project easily? I know, I don't ask for much huh? lol
-
Load your help files and look up "random numbers". It tells how to do it.
The second part should be a challenge that you master.
-
Use this code to get Random numbers.
Code:
Randomize
Num% = Int(Rnd * 100) + 1
-
Here you go
.
Code:
Dim nTry As Integer
Dim nAddCount As Integer
Dim bFound As Integer
Dim nDummy As Integer
Dim MyCollection As New Collection
Randomize
Do Until nAddCount = 15
nTry = Int((100 - 1 + 1) * Rnd + 1)
On Error Resume Next
nDummy = MyCollection.Item(CStr(nTry))
bFound = (Err = 0)
If bFound Then
' It's already in the collection
Err.Clear
Else
' It's not, so add it
MyCollection.Add nTry, CStr(nTry)
nAddCount = nAddCount + 1
End If
Loop
-
Thanks for the help, will give it a try.