Hello, I posted this thread a few days earlier and got help from mirabeau and shg. There are two columns. The second column is the probability of returning the number in the first column.
sample:
[TABLE="class: cms_table"]
<TBODY>[TR]
[TD][/TD]
[TD]probability
[/TD]
[TD]accumulate probability
[/TD]
[/TR]
[TR]
[TD="align: right"]21
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.005681818
[/TD]
[/TR]
[TR]
[TD="align: right"]28
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.011363636
[/TD]
[/TR]
[TR]
[TD="align: right"]30
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.017045455
[/TD]
[/TR]
[TR]
[TD="align: right"]51
[/TD]
[TD="align: right"]0.011363636
[/TD]
[TD="align: right"]0.028409091
[/TD]
[/TR]
[TR]
[TD="align: right"]54
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.034090909
[/TD]
[/TR]
[TR]
[TD="align: right"]55
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.039772727
[/TD]
[/TR]
[TR]
[TD="align: right"]57
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.045454545
[/TD]
[/TR]
[TR]
[TD="align: right"]58
[/TD]
[TD="align: right"]0.011363636
[/TD]
[TD="align: right"]0.056818182
[/TD]
[/TR]
[TR]
[TD="align: right"]63
[/TD]
[TD="align: right"]0.011363636
[/TD]
[TD="align: right"]0.068181818
[/TD]
[/TR]
[TR]
[TD="align: right"]66
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.073863636
[/TD]
[/TR]
[TR]
[TD="align: right"]68
[/TD]
[TD="align: right"]0.011363636
[/TD]
[TD="align: right"]0.085227273
[/TD]
[/TR]
[TR]
[TD="align: right"]70
[/TD]
[TD="align: right"]0.022727273
[/TD]
[TD="align: right"]0.107954545
[/TD]
[/TR]
[TR]
[TD="align: right"]71
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.113636364
[/TD]
[/TR]
[TR]
[TD="align: right"]72
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.119318182
[/TD]
[/TR]
[TR]
[TD="align: right"]74
[/TD]
[TD="align: right"]0.011363636
[/TD]
[TD="align: right"]0.130681818
[/TD]
[/TR]
[TR]
[TD="align: right"]75
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.136363636
[/TD]
[/TR]
[TR]
[TD="align: right"]78
[/TD]
[TD="align: right"]0.017045455
[/TD]
[TD="align: right"]0.153409091
[/TD]
[/TR]
[TR]
[TD="align: right"]79
[/TD]
[TD="align: right"]0.017045455
[/TD]
[TD="align: right"]0.170454545
[/TD]
[/TR]
[TR]
[TD="align: right"]80
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.176136364
[/TD]
[/TR]
</TBODY>[/TABLE]
I want a VBA code to generate random numbers based on these probabilities. mirabeau coded this using VBA sub as follows
I modified this code and made it a function as follows. So i can use the function in the calculation of other functions.
The function works well in the immediate window. it returns random numbers based on the empirical distribution. However, when i use the function in the worksheet, it returns 0 always.
Can anyone tell me where has gone wrong?
Many thanks
Michelle
sample:
[TABLE="class: cms_table"]
<TBODY>[TR]
[TD][/TD]
[TD]probability
[/TD]
[TD]accumulate probability
[/TD]
[/TR]
[TR]
[TD="align: right"]21
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.005681818
[/TD]
[/TR]
[TR]
[TD="align: right"]28
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.011363636
[/TD]
[/TR]
[TR]
[TD="align: right"]30
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.017045455
[/TD]
[/TR]
[TR]
[TD="align: right"]51
[/TD]
[TD="align: right"]0.011363636
[/TD]
[TD="align: right"]0.028409091
[/TD]
[/TR]
[TR]
[TD="align: right"]54
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.034090909
[/TD]
[/TR]
[TR]
[TD="align: right"]55
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.039772727
[/TD]
[/TR]
[TR]
[TD="align: right"]57
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.045454545
[/TD]
[/TR]
[TR]
[TD="align: right"]58
[/TD]
[TD="align: right"]0.011363636
[/TD]
[TD="align: right"]0.056818182
[/TD]
[/TR]
[TR]
[TD="align: right"]63
[/TD]
[TD="align: right"]0.011363636
[/TD]
[TD="align: right"]0.068181818
[/TD]
[/TR]
[TR]
[TD="align: right"]66
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.073863636
[/TD]
[/TR]
[TR]
[TD="align: right"]68
[/TD]
[TD="align: right"]0.011363636
[/TD]
[TD="align: right"]0.085227273
[/TD]
[/TR]
[TR]
[TD="align: right"]70
[/TD]
[TD="align: right"]0.022727273
[/TD]
[TD="align: right"]0.107954545
[/TD]
[/TR]
[TR]
[TD="align: right"]71
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.113636364
[/TD]
[/TR]
[TR]
[TD="align: right"]72
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.119318182
[/TD]
[/TR]
[TR]
[TD="align: right"]74
[/TD]
[TD="align: right"]0.011363636
[/TD]
[TD="align: right"]0.130681818
[/TD]
[/TR]
[TR]
[TD="align: right"]75
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.136363636
[/TD]
[/TR]
[TR]
[TD="align: right"]78
[/TD]
[TD="align: right"]0.017045455
[/TD]
[TD="align: right"]0.153409091
[/TD]
[/TR]
[TR]
[TD="align: right"]79
[/TD]
[TD="align: right"]0.017045455
[/TD]
[TD="align: right"]0.170454545
[/TD]
[/TR]
[TR]
[TD="align: right"]80
[/TD]
[TD="align: right"]0.005681818
[/TD]
[TD="align: right"]0.176136364
[/TD]
[/TR]
</TBODY>[/TABLE]
I want a VBA code to generate random numbers based on these probabilities. mirabeau coded this using VBA sub as follows
Code:
Sub random_distr_2()
Const n& = 100000 'change as required
Dim a, u()
Dim c As Long, j As Long
Dim x As Double, s As Double
ReDim u(1 To n, 1 To 1)
a = Range("A1").CurrentRegion.Resize(, 2)
Randomize
For c = 1 To n
s = 0: x = Rnd
For j = 1 To UBound(a)
s = s + a(j, 2)
If x <= s Then
u(c, 1) = a(j, 1)
Exit For
End If
Next j, c
Range("D2").Resize(n) = u
End Sub
I modified this code and made it a function as follows. So i can use the function in the calculation of other functions.
Code:
Function rcap()
Dim a
Dim j As Long
Dim x As Double, s As Double
a = Range("a1").CurrentRegion.Resize(, 2)
Randomize
s = 0: x = Rnd
For j = 1 To UBound(a)
s = s + a(j, 2)
If x <= s Then
rcap = a(j, 1)
Exit For
End If
Next j
End Function
Can anyone tell me where has gone wrong?
Many thanks
Michelle
Last edited: