n = 104
s0 = 100
u = 1.02
M = 10**5
from random import random
def sim():
    vs, k = 0, 0
    for _ in range(M):
        s = s0
        pr = 1
        for i in range(n):
            p = random()
            if p < 0.495:
                 s *= u
            else:
                s /= u
        if s < s0:
            vs += s0 - s
    print('expected :', vs / M)
    return vs / M

print(sim())
        

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: