[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Four random numbers from a list

Posted on 2007-10-13
7
Medium Priority
?
1,038 Views
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?
0
Comment
Question by:wjshore
  • 3
  • 2
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20073024
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?
0
 

Author Comment

by:wjshore
ID: 20073028
It's not homework.  I need actual code and I'm offering 500 points.  Can someone please help?
0
 

Author Comment

by:wjshore
ID: 20073036
P.S.  I don't need to "display the list" (see offered step 4).  I need to assign the numbers to four variables.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 20073049
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
        Do
            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
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20073052
'assign to your variable'

i.e.

instead of

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

do

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

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20073053
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
    Randomize
    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
  next

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

'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



0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20073300
Thanks wjshore. I guess that this is related to your other question that we were working on.

Good luck with the project.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 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