random numbers

P

Phil Hellmuth

I want to pull a group of random numbers within a certain range, but
don't want to check after each grab whether I've previously pulled the
number. For example, let's say I want to generate three random numbers
between 1 and 100. I'll store the numbers in an array. Can I do this
without having to search through the array each time I generate a new
number to ensure I haven't already pulled the number?

Thanks in advance.
 
S

Steve

Random does not mean exclusive. If you want to generate three random numbers
between 1 and 100, the result could be 99, 99, 99 although the probability
of getting that result is low. It's like flipping a coin one hundred times,
there is a chance of getting one hundred heads.

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
 
J

John W. Vinson

I want to pull a group of random numbers within a certain range, but
don't want to check after each grab whether I've previously pulled the
number. For example, let's say I want to generate three random numbers
between 1 and 100. I'll store the numbers in an array. Can I do this
without having to search through the array each time I generate a new
number to ensure I haven't already pulled the number?

Thanks in advance.

As Steve says, that's not really a random selection; it's a random *ordering*
("select without replacement" in statistics jargon. You can get the effect by
having a table of your random numbers and shuffling it, sorting the records by
a random value. You can store them in a table and have some field (yes/no
perhaps) indicating which ones you've already selected.

John W. Vinson [MVP]
 
G

Guest

If you want this to be like a lottery pull (numbers pulled from finite set in
random order) you might use something like this:

Public Function LotteryPull()

Dim xPOS As Long
Dim xMIN As Long
Dim xMAX As Long
Dim zNUMS As Long
Dim iCounter As Long
Dim jCounter As Long

Dim mAry As Variant
Dim aryAnswer() As Variant

xMIN = 1 'bottom of range
xMAX = 100 'top of range
zNUMS = 3 'count of numbers you want

ReDim mAry(xMIN To xMAX)

jCounter = 0

Do
Randomize ' Initialize random-number generator.

xPOS = CLng(((xMAX - xMIN) * Rnd) + xMIN)
If IsEmpty(mAry(xPOS)) Then
mAry(xPOS) = xPOS
jCounter = jCounter + 1
ReDim Preserve aryAnswer(jCounter)
aryAnswer(jCounter) = xPOS

End If

Loop While jCounter < zNUMS

For iCounter = LBound(aryAnswer) To UBound(aryAnswer)
Debug.Print aryAnswer(iCounter)
'these are not sorted, they are in the order pulled
Next


End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top