samedi 14 septembre 2019

Why is this code not giving any duplicates?

I was looking for a lottery number generator and I found this:

Sub LotteyCode()
Dim Rng As Range
Dim WorkRng As Range
Dim xNumbers(49) As Integer
On Error Resume Next
For xy = 1 To 10000
Set WorkRng = Range("A" & xy)
For xIndex = 1 To 49
xNumbers(xIndex) = xIndex
Next
For xIndex = 1 To 6
xNum = 1 + Application.Round(Rnd * (49 - xIndex), 0)
WorkRng.Offset(0, xIndex - 1).Value = xNumbers(xNum)
xNumbers(xNum) = xNumbers(50 - xIndex)
Next
Next xy
End Sub

I read through the code and for the life of me I cannot understand how this is NOT generating duplicate numbers (the code didn't come with explanation on why it was written like this)...

I ran 10000 iterations (as you can see by the "xy = 1 To 10000") to make sure it really is giving unique numbers.

If anyone can shed some light on this, I would be most greatful!




Aucun commentaire:

Enregistrer un commentaire