Hi,
Hereafter the code to generate 1000 random numbers and take the 20 with
highest occurence.
I have to thank Rick Rothstein and Chip Pearson for their feedback on
collections and their advise, but a special thank to Dana De Louis who
provided me some sample code for the use of dictionaries. As you can see,
I've made use of it.
The bubble sort routine was found on following page:
http://www.schouppe.net/comlog/tabellen/sorteren/bubbleSort.htm
(this page is in Dutch, the sample code also, my only contribution was to
translate the code in English and changing the sorting order from ascending
to descending).
Trying to answer your question was also a very instructive experiment for
myself.
Wkr,
JP
Sub Random2()
Dim dicRnd As Object
Dim intRnd As Integer
Dim i As Integer
Dim varKeys As Variant
Dim varValues As Variant
Dim arr()
Dim intTmp As Integer
Dim blnSorted As Boolean
Dim intUnsorted As Integer
Set dicRnd = CreateObject("Scripting.Dictionary")
For i = 1 To 1000
intRnd = Int((100 - 1 + 1) * Rnd + 1)
If dicRnd.Exists(intRnd) Then
dicRnd(intRnd) = dicRnd(intRnd) + 1
Else
dicRnd.Add Key:=intRnd, Item:=1
End If
Next i
varKeys = dicRnd.Keys
varValues = dicRnd.Items
ReDim arr(0 To UBound(varKeys), 1 To 2)
For i = 0 To UBound(varKeys)
arr(i, 1) = varKeys(i): arr(i, 2) = varValues(i)
Next i
blnSorted = False
intUnsorted = UBound(varKeys)
Do While (blnSorted = False And intUnsorted > 0)
blnSorted = True
intUnsorted = intUnsorted - 1
i = 0
Do While i <= intUnsorted
If arr(i, 2) < arr(i + 1, 2) Then
intTmp = arr(i, 1)
arr(i, 1) = arr(i + 1, 1)
arr(i + 1, 1) = intTmp
intTmp = arr(i, 2)
arr(i, 2) = arr(i + 1, 2)
arr(i + 1, 2) = intTmp
blnSorted = False
End If
i = i + 1
Loop
Loop
''' uncomment this for testing
'''For i = 1 To 99
''' Cells(i, 1) = varKeys(i - 1): Cells(i, 2) = varValues(i - 1):
Cells(i, 3) = arr(i - 1, 1): Cells(i, 4) = arr(i - 1, 2)
'''Next i
''' end uncomment
''' comment this for testing
For i = 0 To 19
Cells(i + 1, 1) = arr(i, 1)
Next i
''' end comment
End Sub
Hello,
Your second question is not clear to me, what do you mean with the top 20
numbers? 20 most frequently generated numbers?
Thanks for your reply.
When a 1,000 random numbers are displayed, using a 1,000 rows,
take the frequency of the 1000 numbers and display on the top 20
most frequent numbers,different numbers. I suspect some numbers
would be repeated several times. Thereby, using only 20 rows not 1,000
rows
With thanks