Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Array 1

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hi, I have some code here for randomizing an array of names. How can I make the array so that it will NOT repeat names?

Code:
Dim nameArr
nameArr = Array("Mike","John","Ken","Mark","Kim","Cindy","Joe")

Randomize
For i = 0 To 2
MsgBox nameArr(Int(Rnd*7))
Next

Thanks for any help!
 
What about this ?
Code:
nameArr = Array("Mike", "John", "Ken", "Mark", "Kim", "Cindy", "Joe")
numArr = Array(-1, -1, -1)
Randomize
For i = 0 To 2
    j = Int(Rnd * 7)
    For k = 0 To i
        If numArr(k) = j Then Exit For
    Next
    If k > i Then
        numArr(i) = j
        MsgBox nameArr(j)
    Else
        i = i - 1
    End If
Next

Another way is to use a Dictionary object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks, PHV ... I think this just what I need.

I am still quite new to VBS and was wondering if you might explain this code a little bit. I really appreciate your time and help.

-Mike
 
The following thread discusses pulling random numbers from a text file, similar to your problem of pulling them from an array.

thread329-1680222
 
Thanks for the info jges.... that is a great thread and the code could come in handy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top