Heenan, Lambert
Lambert.Heenan at AIG.com
Thu Mar 3 15:36:26 CST 2005
Ok. If you'd prefer to use a collection instead of an array....
Function GetRandomCol(nNumberOfSelections As Long, nSelectFrom As Long) As
Collection
Dim c As New Collection
Dim n As Long
Dim nHits As Long
Dim nRnd As Long
Dim bHit As Boolean
nHits = 0
Randomize
While nHits < nNumberOfSelections
nRnd = Int(Rnd() * nSelectFrom)
bHit = False
For n = 1 To c.Count
If c.Item(n) = nRnd Then
bHit = True
Exit For
End If
Next n
If bHit = False Then
c.Add nRnd ' no need for a Key here as we will use the item()
array to get at our members
nHits = nHits + 1
End If
Wend
Set GetRandomCol = c
End Function
Sub testRndColl()
Dim c As Collection
Dim n As Long
Set c = GetRandomCol(5, 29)
For n = 1 To c.Count
Debug.Print c.Item(n) & ", ";
Next n
Debug.Print
End Sub
Lambert
> -----Original Message-----
> From: accessd-bounces at databaseadvisors.com
> [SMTP:accessd-bounces at databaseadvisors.com] On Behalf Of Steve Erbach
> Sent: Thursday, March 03, 2005 3:18 PM
> To: Access Developers discussion and problem solving
> Subject: Re: [AccessD] Random numbers lottery-style
>
> According to the documentation for the Remove method of a Collection,
> the index of the collection is, indeed, renumbered. That just might
> fill the bill. Thanks, Jim.
>
> Steve Erbach
>
>
> On Thu, 3 Mar 2005 14:03:11 -0600, Steve Erbach <erbachs at gmail.com> wrote:
> > JIm,
> >
> > I'd forgotten about collections. So, lets say we have a collection like
> thise:
> >
> > col(1) = 16
> > col(2) = 18
> > col(3) = 27
> > col(4) = 29
> > ...
> > col(29) = 147
> >
> > So we generate a random number from 1 to 29 and zoom in on that index
> > in the collecition. If the selected number is 4 then I pick col(4) and
> > then remove that item from the collection for the next go round.
> >
> > But how, then, do I pick the Nth collection item if the indexes are
> > non-continguous? Or does the collection re-index itself after removing
> > an item?
> >
> > Steve Erbach
> >
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com