Collection for random numbers

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,341
I create a single value which is the sum of various random numbers. I repeat this up to 100,000 times and currently I insert them in a worksheet and sort the results. I can then calculate the minimum, maximum, mean, mode, 5th percentile, etc. I'm wondering if I could speed up the process by sending the results to a Collection, a Dictionary or an Array. Does anyone have any thoughts about this?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
It's not clear how you're currently doing this, or what "process" it is that you're looking to speed up.

But if you are using VBA code to write a single result to Excel, times 100,000 iterations, yes, it would be much faster to store the 100,000 results in a VBA array and write this array once to Excel.
 
Upvote 0
Hi Stephen,
many thanks for the response. Yes, I am writing each value as it is created and it seemed to me that this was inefficient. Thank you for your reply, it is very much appreciated.
 
Upvote 0
VBA Code:
'Compare
Sub Slow()

    Dim i As Long
    Dim t As Double
    
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    t = Timer
    
    For i = 1 To 100000
        Range("A" & i).Value = i
    Next i
    
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    MsgBox Timer - t & " seconds"

End Sub
Sub Fast()

    Dim x(1 To 100000, 1 To 1) As Long, i As Long
    Dim t As Double

    t = Timer
    
    For i = 1 To 100000
        x(i, 1) = i
    Next i
    
    Range("A1:A100000").Value = x
    MsgBox Timer - t & " seconds"

End Sub
 
Upvote 0
Impressive Stephen. I have a very old computer with a large key at the back that you have to wind up, yet the "Fast" version ran almost instantaneously. It took 3.5% of the time taken by the slower version which is an exact match of what I was using. Thanks you very much for the example it is very useful. Is it possible to modify the "Fast" version to run a variable number of times? It didn't like Dim x(1 To y, 1 To 1)
 
Upvote 0
You can't declare using a variable size. But you can do this:

VBA Code:
Dim x() 'allocate me an array, size to be specified later

'...
y = 100000  'say
ReDim x(1 To y, 1 To 1)
 
Upvote 0
The alternative solution:
1) ask user to input the limit numbers
2) generate random numbers from 1 to limitation
3) Sort the result
For 10,000 rows, it took under 1 second. For 1 milion rows, 7 seconds.
VBA Code:
Option Explicit
Sub test()
Dim t As Double, ib As Double
t = Timer
ib = InputBox("How many random numbers do you like to get?")
If Not IsNumeric(ib) Or ib > Cells(Rows.Count, "A").Row Then
    MsgBox "Invalid number"
    Exit Sub
End If
Columns(1).ClearContents ' clear existing value in column A
With Range("A2:A" & ib + 1)
    .Formula = "=randbetween(1," & ib & ")" ' generate random values from 1 to input value
    .Value = .Value ' remove the formula then get the value only
    .Sort Range("A1") ' sort the result. Remove it if you dont want sorted values.
End With
MsgBox Timer - t & " seconds"
End Sub
 
Upvote 0
I think I lost track of what the goal of the OP is.

OP mentions Mean, Median, etc. The rest of the thread took off to generate all random numbers between 2 digits. ???
 
Upvote 0
I think I lost track of what the goal of the OP is.
OP mentions Mean, Median, etc. The rest of the thread took off to generate all random numbers between 2 digits. ???
I guess OP need help in 1st step: generate random list.
The final step (Mean, median,...) he could manage himself.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top