If you are interested to know what is the probability to get any number between 1 and 100 with such draw, here is code you can test in playground.
The logic is to compute Probability that the draw is n, with n between 1 and 100
This is the probability P(x = n), computed in
Code Block func P(_ n: Int) -> Double |
This probability is computed as:
Sum ( Probability(r = m) * Probability(v = n-m)
Where
This probability is zero out of [first...second] and 1 / (second - first + 1) inside
This is computed in
Code Block func pR(_ r: Int) -> Double |
This probability is zero out of [-variability...variability] and 1 / (2*variability+1) inside
This is computed in
Code Block func pV(_ v: Int) -> Double |
Sum ( Probability(r = m) * Probability(v = n-m) is computed in
Code Block func P(_ n: Int) -> Double { |
var r = 0.0 |
for m in firstNumber...secondNumber { |
r += pR(m) * pV(n - m) |
} |
return r |
} |
Here is the full code for computation:
Code Block let firstNumber = 20 |
let secondNumber = 30 |
let variabilityRange = 12 |
|
func pR(_ r: Int) -> Double { |
if r < firstNumber r > secondNumber { return 0.0 } |
return 1/Double(secondNumber - firstNumber + 1) |
} |
|
func pV(_ v: Int) -> Double { |
if v < -variabilityRange v > variabilityRange { return 0.0 } |
return 1/(Double(2 * variabilityRange + 1)) |
} |
|
func P(_ n: Int) -> Double { |
var r = 0.0 |
for m in firstNumber...secondNumber { |
r += pR(m) * pV(n - m) |
} |
return r |
} |
|
var sigma = 0.0 |
for x in 1...100 { |
print(x, " :", P(x)) |
sigma += P(x) |
} |
print("Sigma", sigma) |
which yields
<= 7 : 0.0
8 : 0.0036363636363636364
9 : 0.007272727272727273
10 : 0.01090909090909091
11 : 0.014545454545454545
12 : 0.01818181818181818
13 : 0.021818181818181816
14 : 0.025454545454545452
15 : 0.029090909090909087
16 : 0.03272727272727272
17 : 0.03636363636363636
18 : 0.04
19 : 0.04
20 : 0.04
21 : 0.04
22 : 0.04
23 : 0.04
24 : 0.04
25 : 0.04
26 : 0.04
27 : 0.04
28 : 0.04
29 : 0.04
30 : 0.04
31 : 0.04
32 : 0.04
33 : 0.03636363636363636
34 : 0.03272727272727272
35 : 0.029090909090909087
36 : 0.025454545454545452
37 : 0.021818181818181816
38 : 0.01818181818181818
39 : 0.014545454545454545
40 : 0.01090909090909091
41 : 0.007272727272727273
42 : 0.0036363636363636364
>= 43 : 0.0
Sigma 1.0000000000000002
Note:
you see it grows linearity from 8 to 18
Then fix value of 0.04
and decrease linearily from 33 to 42.
That makes the computation absolutely immediate, without heavy computation.
With different initial data that may pass over 100:
Code Block let firstNumber = 80 |
let secondNumber = 90 |
let variabilityRange = 15 |
You get
<=64 : 0.0
65 : 0.002932551319648094
66 : 0.005865102639296188
67 : 0.00879765395894428
68 : 0.011730205278592375
69 : 0.01466275659824047
70 : 0.017595307917888565
71 : 0.02052785923753666
72 : 0.023460410557184754
73 : 0.02639296187683285
74 : 0.029325513196480944
75 : 0.03225806451612904
76 : 0.03225806451612904
77 : 0.03225806451612904
78 : 0.03225806451612904
79 : 0.03225806451612904
80 : 0.03225806451612904
81 : 0.03225806451612904
82 : 0.03225806451612904
83 : 0.03225806451612904
84 : 0.03225806451612904
85 : 0.03225806451612904
86 : 0.03225806451612904
87 : 0.03225806451612904
88 : 0.03225806451612904
89 : 0.03225806451612904
90 : 0.03225806451612904
91 : 0.03225806451612904
92 : 0.03225806451612904
93 : 0.03225806451612904
94 : 0.03225806451612904
95 : 0.03225806451612904
96 : 0.029325513196480944
97 : 0.02639296187683285
98 : 0.023460410557184754
99 : 0.02052785923753666
100 : 0.017595307917888565
Sigma 0.9560117302052784
Same note applies here.
You see that if you trim values over 100 to 100, that gives a probability of 0.06158357771261014 to get 100
which is no more a random distribution
Redoing the draw if it is over 100 leads to multiply evenly each probability by 1/ 0.9560117302052784 which is around 1.046
Then Probability for 100 is 0,0184 (versus 0,0616)