[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
Medium Priority
1,038 Views
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
Question by:wjshore
• 3
• 2
• 2

LVL 143

Expert Comment

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

ID: 20073028
0

Author Comment

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

LVL 76

Accepted Solution

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

ID: 20073052

i.e.

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

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

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

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