Four random numbers from a list

Posted on 2007-10-13
Last Modified: 2013-11-26
I'm looking for a specific kind of number choosing routine.  The idea is to choose 4 DIFFERENT numbers from a list of consecutive numbers.  It should begin with a a value (variable) called something like ListLength.  If we assume ListLength = 20, the routine would choose 4 values (val1, val2, val3, val4) - all between 1 and 20 and all different from each other.  Can someone please show me how?
Question by:wjshore
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    as I am not sure if this is homework or not, I will do as if it were homework, giving guidance.

    step 1:
    you want random numbers
     -> do you know how to generate a random number?

    step 2:
    you want multiple random numbers
     -> means, you have to repeat "step 1"
     -> what vb language concepts could you use for that?

    step 3:
    you want distinct random numbers
     -> for each number you generate, you have to check if that was not already generated
     -> means, you have to "remember" the list of already generated numbers (which you would need to do anyhow)
     => if you find the number generated in that list, you have to repeat the process of finding a new random number

    step 4:
     -> display the list.

    where is the problem, actually, once you now know all that?

    Author Comment

    It's not homework.  I need actual code and I'm offering 500 points.  Can someone please help?

    Author Comment

    P.S.  I don't need to "display the list" (see offered step 4).  I need to assign the numbers to four variables.
    LVL 76

    Accepted Solution

    This puts them in a array. You can assign the array element s to your variables if you wish

    Sub FourRandom()
        Dim iNumbers(3) As Integer
        Dim i As Integer
        Dim j As Integer
        Dim iTest As Integer
        Dim ListLength As Integer
        Dim bFound As Boolean
        ListLength = 20
        For i = 0 To 3
        bFound = False
                iTest = 1 + Int(Rnd * ListLength)
                For j = 0 To i - 1
                If iTest = Rnd(i) Then
                    bFound = True
                    Exit For
                End If
                Next j
            Loop Until bFound = False
            iNumbers(i) = iTest
        Next i
        For j = 0 To 3
            Debug.Print iNumbers(j)
        Next j
    End Sub
    LVL 76

    Expert Comment

    'assign to your variable'


    instead of

        For j = 0 To 3
            Debug.Print iNumbers(j)
        Next j
    End Sub


    Var1 = iNumbers(0)
    Var2 = iNumbers(1)
    Var3 = iNumbers(2)
    Var4 = iNumbers(3)
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    ok, here we go:

    create this function:
    Function Random(Lowerbound  As Long, Upperbound  As Long)
      static has_randomized as boolean
      if has_randomized = false then
        has_randomized  = true
      end if
      Random = Int(Rnd * Upperbound) + Lowerbound
    End Function

    that will allow you to get 1 random number, between 1 and 20
    Debug.print Random(1,20)

    now, you want 4 of them:
    Dim rndValues(1 to 4) as integer

    now, loop to generate 4 random numbers, checking all the previous ones
    dim lngLoop as long
    dim lngCheck as long
    dim blnMatch as boolean

    lngLoop = lbound(rndValues)
    while lngLoop <= ubound(rndValues)
      'generate a number
      rndValues(lngLoop) = Random(1,20)  

      'check with previous values
      blnMatch = false
      for lngCheck = lbound(rndValues) to lngLoop - 1
         if rndValues(lngCheck) = rndValues(lngLoop) then
            blnMatch = true
         end if

      'if already found, continue, otherwise go to next number
      if not blnMatch then
          lngLoop = lngLoop + 1
      end if

    'here, you have the 4 random values in the array rndValues
    'if you wish to have more than 4 values, increase the array from 4 to that number

    LVL 76

    Expert Comment

    Thanks wjshore. I guess that this is related to your other question that we were working on.

    Good luck with the project.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now