Re: Random Numbers

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


"smandula" <smandula@xxxxxxxxxxx> wrote in message
news:06b65b18-32f0-42b4-aa1c-1f7016cb10ab@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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






.



Relevant Pages

  • DVB/ATSC Sample Code
    ... Does anyone have any suggestions for pointing me to some sample code ... I would like to be able to display a non digital channel (i.e. channel ... Multiple digital TV cards (MyHD PCI, ...
    (microsoft.public.win32.programmer.directx.video)
  • RE: DataGrid - how to configure columns in C# code?
    ... You can download some sample code which illustrates the same here ... "Dmitry Bond." ... > I have a DataSet with some data to display in DataGrid. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Display multiple records on a single form
    ... I have an access/vba application I'm trying to extend. ... potentially have any number of records which I'd like to display on a ... this type of form and possibly some sample code (or a URL to some sample ... controls I'd need to use to facilitate this functionality, ...
    (microsoft.public.access.gettingstarted)
  • Re: Display multiple records on a single form
    ... I have an access/vba application I'm trying to extend. ... potentially have any number of records which I'd like to display on a ... this type of form and possibly some sample code (or a URL to some sample ... controls I'd need to use to facilitate this functionality, ...
    (microsoft.public.access.forms)