“Few things are harder to put up with than the annoyance of a good example.” [Mark Twain]
Abstract
Manchmal benötigt man ganze Zufallszahlen, die sich nicht (oder nur begrenzt häufig) wiederholen.


Appendix – UniqRandInt Code
Bitte den Haftungsausschluss im Impressum beachten.
'If lRange >> n then set LATE_INITIALISATION to true. For example,
'if lRange=1,000,000 and if 1,000 cells are selected (n=1000).
#Const LATE_INITIALISATION = True
'If random integers may occur more than once, allow repetitions
#Const ALLOW_REPETITION = True
#If ALLOW_REPETITION Then
Function UniqRandInt(n As Long, ByVal lRange As Long, _
Optional lMaxOccurence As Long = 1) As Variant
#Else
Function UniqRandInt(n As Long, ByVal lRange As Long) As Variant
#End If
'Returns n unique (=non-repeating) random integers within 1..lRange,
'lRange >= n. Set ALLOW_REPETITION = True and call with
'lMaxOccurences > 1 if random integers may occur more than once.
'Source (EN): http://www.sulprobil.de/uniqrandint_en/
'Source (DE): http://www.berndplumhoff.de/uniqrandint_de/
'(C) (P) by Bernd Plumhoff 30-Oct-2024 PB V1.04
Static bRandomized As Boolean
Dim vA As Variant
Dim vR As Variant
Dim i As Long
Dim j As Long
Dim lr As Long
If Not bRandomized Then
Randomize
bRandomized = True
End If
#If ALLOW_REPETITION Then
If lMaxOccurence < 1 Then
UniqRandInt = CVErr(xlErrNum)
Exit Function
End If
lRange = lRange * lMaxOccurence
#End If
If n > lRange Then
UniqRandInt = CVErr(xlErrValue)
Exit Function
End If
ReDim vR(1 To n) As Variant
ReDim vA(1 To lRange)
#If Not LATE_INITIALISATION Then
For i = 1 To lRange
#If ALLOW_REPETITION Then
vA(i) = Int((i - 1) / lMaxOccurence) + 1
#Else
vA(i) = i
#End If
Next i
#End If
i = 1
For j = 1 To UBound(vR, 1)
lr = Int(((lRange - i + 1) * Rnd) + 1)
#If LATE_INITIALISATION Then
If vA(lr) = 0 Then
#If ALLOW_REPETITION Then
vR(j) = Int((lr - 1) / lMaxOccurence) + 1
#Else
vR(j) = lr
#End If
Else
#End If
vR(j) = vA(lr)
#If LATE_INITIALISATION Then
End If
If vA(lRange - i + 1) = 0 Then
#If ALLOW_REPETITION Then
vA(lr) = Int((lRange - i + 1 - 1) / lMaxOccurence) + 1
#Else
vA(lr) = lRange - i + 1
#End If
Else
#End If
vA(lr) = vA(lRange - i + 1)
#If LATE_INITIALISATION Then
End If
#End If
i = i + 1
Next j
UniqRandInt = vR
End Function
Download
Bitte den Haftungsausschluss im Impressum beachten.
UniqRandInt.xlsm [39 KB Excel Datei, ohne jegliche Gewährleistung]