Probabilistically Running Based On Hour
<p>I have a function that I want to run with increasing probability each hour until noon then decreasing probability until midnight. I can imagine a normal distribution centered on noon would do it (so the probability of running the function is 100% at noon but very low at midnight), however I cannot convert that into python code.</p>

<p>For example, if the code is executed at 1am it has a very low probability of running. But if the code is executed at noon, it has a 100% probability of running.</p>

<p>This is a completely crude, unpythonic, manual attempt at what I want:</p>

<pre><code>currentHour = 12

if currentHour == 1:
    print('probability = 0')
elif currentHour == 2:
    print('probability = 0')
elif currentHour == 3:
    print('probability = .1')
elif currentHour == 4:
    print('probability = .2')
elif currentHour == 5:
    print('probability = .3')
elif currentHour == 6:
    print('probability = .4')
elif currentHour == 7:
    print('probability = .5')
elif currentHour == 8:
    print('probability = .6')
elif currentHour == 9:
    print('probability = .7')
elif currentHour == 10:
    print('probability = .8')
elif currentHour == 11:
    print('probability = .9')
elif currentHour == 12:
    print('probability = 1')
elif currentHour == 13:
    print('probability = .9')
elif currentHour == 13:
    print('probability = .8')
elif currentHour == 14:
    print('probability = .7')
elif currentHour == 15:
    print('probability = .6')
elif currentHour == 16:
    print('probability = .5')
elif currentHour == 17:
    print('probability = .4')
elif currentHour == 18:
    print('probability = .3')
elif currentHour == 19:
    print('probability = .2')
elif currentHour == 20:
    print('probability = .1')
elif currentHour == 21:
    print('probability = 0')
elif currentHour == 22:
    print('probability = 0')
elif currentHour == 23:
    print('probability = 0')
elif currentHour == 24:
    print('probability = 0')
</code></pre>

<p>Lots of options. Just make a function that returns a value between 0 and 1 based on the hour. Then, make a random float between 0 and 1. If the float is less than the probability, run the program.</p>

<pre><code>import numpy as np

def prob_sawtooth(hour):
    return 1. - abs((hour - 12.) / 12.)

def prob_sin(hour):
    return np.sin(hour / 24. * np.pi)

def prob_gaussian(hour, stdev=6.):
    gauss = lambda x, m, s: np.exp(-(x-m)**2 / (2*s**2)) / np.sqrt(2*np.pi*s**2)
    return gauss(hour, 12., stdev) / gauss(12., 12., stdev)

test = np.random.rand()
#14:00
if test &lt;= prob_gaussian(14.):
    # run program
    pass
</code></pre>

python