FBxiii said:
Hi.
If I have a 'pot' of numbers 1-50 say, how would I select each of these
numbers at random until they have all been used up?
(I think I know the answer but would like a professional opinion
Cheers,
Steve.
This is called a "random permutaion". Below is an algorithm (dating
back to 1964!). Use it this way:
Dim aPerm As Variant
Dim i As Integer
aPerm = RandomPermutation(50)
For i = 1 To 50
Debug.Print aPerm(i)
Next
HTH
Mtthias Kläy
--
www.kcc.ch
Public Function RandomPermutation(ByVal N As Long) As Variant
'---------------------------------------------------------------------------------------
' Random permutation of numbers 1 to N.
' Durstenfeld R., Random Permutation. CACM July 1964 No. 7, p. 420
'
Dim aItem As Variant
Dim i As Long
Dim k As Long
Dim j As Long
' Fill array aItem with numbers 1 to N
ReDim aItem(1 To N) As Long
For i = 1 To N
aItem(i) = i
Next
' Initialize the random number generator
Randomize
' Permute
For i = N To 2 Step -1
j = CLng(Int(CDbl(i) * Rand)) + 1
k = aItem(i)
aItem(i) = aItem(j)
aItem(j) = k
Next
RandomPermutation = aItem
End Function