Results 1 to 5 of 5

Thread: Does anyone have the code to do this?...

  1. #1
    Guest
    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

  2. #2
    Addicted Member
    Join Date
    Mar 2000
    Location
    Gainesville, FL
    Posts
    131
    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.

  3. #3
    Guest
    Use this code to get Random numbers.

    Code:
    Randomize
    Num% = Int(Rnd * 100) + 1

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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

  5. #5
    Guest
    Thanks for the help, will give it a try.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width