I read through this thread and then through the help file on Randomize. If I understand them correctly, it seems to indicate that if you run a macro that has a rnd call in it it picks the seed the first time you run it, then if you don't use Randomize or completely close and reopen Excel, it reuses the seed? Is that accurate? I ask because I am trying find a way to squash an intermittent bug. I can't even figure out how to get it to recur consistently so I'm hoping that someone will look at this code or questions and know what is causing it.
Basically, I am generating a random 6 digit number between 100,000 and 999,999 for the purposes of a raffle. I read the already generated numbers into an array, then generate a random number. If it exists, I add one to it and recheck. Rinse, repeat if necessary. BUT for some reason intermittently I get 734992 and 580081 repeatedly. This doesn't happen most the time but I'd like it not to happen at all. If I get a duplicate number I log the original duplicate in column G so that I can see what the original duplicated number was. I fully expect to get some duplicates but once I got one of those 2 numbers 40 times in a row. That was with 10-15 minutes in between runs of the macro... which goes to "End Sub" before running again.
Now I know random numbers on computers are actually "random" numbers but that seems a bit extreme. That isn't psuedorandom in the least. ...
What am I missing? Is it really as simple as putting the Randomize statement before the random number generator? Again, if it wasn't intermittent, I wouldn't ask, I'd just try it and other things until I squished it myself. Or I'd start a question like this and in explaining it I'd end up stumbling over the answer... that happens 98% of the time it seems like. LOL
Thanks in advance for any assistance!
lDuplicate = 0
'read existing numbers into array
Sheets(2).Activate
With Sheets("Sheet2")
LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
aEntries = Range(Cells(2, 6), Cells(LastRow, 6))
End With
' read data into array and generate random entry number
Sheets(1).Activate
With Sheets("Sheet1")
aEntryInfo = Range("c7:c16") ' read entrant info into array
lEntryNumber = Int((999999 - 100000 + 1) * Rnd + 100000) 'generate a random number
While IsInArr(lEntryNumber, aEntries) = True
If lDuplicate = 0 Then lDuplicate = lEntryNumber
lEntryNumber = lEntryNumber + 1
If lEntryNumber = 1000000 Then lEntryNumber = 100000'if adding 1 made it 7 digits
Wend
End With
'Put data into appropriate cells
Sheets(2).Activate
With Sheets("Sheet2")
iLastRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1 ' find first empty cell in column A
Sheets("Sheet2").Cells(iLastRow, 6).Value = lEntryNumber
Sheets("Sheet2").Cells(iLastRow, 7).Value = lDuplicate
End With
Function IsInArr(lToBeFound As Long, arr As Variant)
Dim byDimension As Byte, i As Long, j As Long
On Error Resume Next
If IsError(UBound(arr, 2)) Then byDimension = 1 Else byDimension = 2
On Error GoTo 0
Select Case byDimension
Case 1
For j = LBound(arr) To UBound(arr)
If arr(j) = lToBeFound Then IsInArr = True: Exit Function
Next
Case 2
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
If arr(i, j) = lToBeFound Then IsInArr = True: Exit Function
Next j
Next i
End Select
End Function
Aucun commentaire:
Enregistrer un commentaire