Hello
I have several probability tables of variables. I want to generate random numbers based on the probabilities. I used the function below to generate random numbers. However, this function took a long time to manually type every probability, and I got several variables which need to be generated. Can anyone help me to improve the function so that it can read the probabilities directly from the table or are there other ways to generate random numbers based on empirical distribution?
sample:
[TABLE="width: 314"]
<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]
Many thanks
Michelle
I have several probability tables of variables. I want to generate random numbers based on the probabilities. I used the function below to generate random numbers. However, this function took a long time to manually type every probability, and I got several variables which need to be generated. Can anyone help me to improve the function so that it can read the probabilities directly from the table or are there other ways to generate random numbers based on empirical distribution?
sample:
[TABLE="width: 314"]
<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]
Code:
Function CapacityP() As Double
Randomize
Dim r As Double
r = Rnd
Select Case r
Case 0 To 0.005681818
CapacityP = 21
Case 0.005681818 To 0.011363636
CapacityP = 28
Case 0.011363636 To 0.017045455
CapacityP = 30
Case 0.017045455 To 0.028409091
CapacityP = 51
Case 0.028409091 To 0.034090909
CapacityP = 54
Case 0.034090909 To 0.039772727
CapacityP = 55
Case 0.039772727 To 0.045454545
CapacityP = 57
Case 0.045454545 To 0.056818182
CapacityP = 58
Case 0.056818182 To 0.068181818
CapacityP = 63
Case 0.068181818 To 0.073863636
CapacityP = 66
Case 0.073863636 To 0.085227273
CapacityP = 68
Case 0.085227273 To 0.107954545
CapacityP = 70
Case 0.107954545 To 0.113636364
CapacityP = 71
Case 0.113636364 To 0.119318182
CapacityP = 72
Case 0.119318182 To 0.130681818
CapacityP = 74
Case 0.130681818 To 0.136363636
CapacityP = 75
Case 0.136363636 To 0.153409091
CapacityP = 78
Case 0.153409091 To 0.170454545
CapacityP = 79
Case 0.170454545 To 0.176136364
CapacityP = 80
Case 0.176136364 To 0.204545455
CapacityP = 81
Case 0.204545455 To 0.215909091
CapacityP = 82
Case 0.215909091 To 0.221590909
CapacityP = 84
Case 0.221590909 To 0.227272727
CapacityP = 85
Case 0.227272727 To 0.238636364
CapacityP = 86
Case 0.238636364 To 0.255681818
CapacityP = 87
Case 0.255681818 To 0.261363636
CapacityP = 88
Case 0.261363636 To 0.272727273
CapacityP = 89
Case 0.272727273 To 0.284090909
CapacityP = 90
Case 0.284090909 To 0.295454545
CapacityP = 91
Case 0.295454545 To 0.306818182
CapacityP = 92
Case 0.306818182 To 0.329545455
CapacityP = 93
Case 0.329545455 To 0.363636364
CapacityP = 94
Case 0.363636364 To 0.392045455
CapacityP = 95
Case 0.392045455 To 0.426136364
CapacityP = 96
Case 0.426136364 To 0.443181818
CapacityP = 97
Case 0.443181818 To 0.465909091
CapacityP = 98
Case 0.465909091 To 0.482954545
CapacityP = 99
Case 0.482954545 To 0.494318182
CapacityP = 100
Case 0.494318182 To 0.511363636
CapacityP = 101
Case 0.511363636 To 0.528409091
CapacityP = 102
Case 0.528409091 To 0.5625
CapacityP = 103
Case 0.5625 To 0.585227273
CapacityP = 104
Case 0.585227273 To 0.596590909
CapacityP = 105
Case 0.596590909 To 0.613636364
CapacityP = 106
Case 0.613636364 To 0.630681818
CapacityP = 107
Case 0.630681818 To 0.636363636
CapacityP = 108
Case 0.636363636 To 0.664772727
CapacityP = 109
Case 0.664772727 To 0.681818182
CapacityP = 110
Case 0.681818182 To 0.710227273
CapacityP = 111
Case 0.710227273 To 0.75
CapacityP = 112
Case 0.75 To 0.761363636
CapacityP = 113
Case 0.761363636 To 0.778409091
CapacityP = 114
Case 0.778409091 To 0.801136364
CapacityP = 115
Case 0.801136364 To 0.818181818
CapacityP = 116
Case 0.818181818 To 0.840909091
CapacityP = 117
Case 0.840909091 To 0.852272727
CapacityP = 118
Case 0.852272727 To 0.857954545
CapacityP = 119
Case 0.857954545 To 0.875
CapacityP = 121
Case 0.875 To 0.892045455
CapacityP = 122
Case 0.892045455 To 0.897727273
CapacityP = 124
Case 0.897727273 To 0.909090909
CapacityP = 125
Case 0.909090909 To 0.914772727
CapacityP = 129
Case 0.914772727 To 0.926136364
CapacityP = 130
Case 0.926136364 To 0.943181818
CapacityP = 131
Case 0.943181818 To 0.954545455
CapacityP = 132
Case 0.954545455 To 0.960227273
CapacityP = 133
Case 0.960227273 To 0.965909091
CapacityP = 135
Case 0.965909091 To 0.977272727
CapacityP = 139
Case 0.977272727 To 0.982954545
CapacityP = 140
Case 0.982954545 To 0.988636364
CapacityP = 141
Case 0.988636364 To 0.994318182
CapacityP = 142
Case 0.994318182 To 1
CapacityP = 151
Case Else
CapacityP = 0
End Select
End Function
Many thanks
Michelle