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>Pure magic. I figured it out. No, just joking. When you asked that question, it immediately reminded me of sinus waves, they go up and then down again - just like your thing you're trying to do.</p>

<p>According to <a href="http://jwilson.coe.uga.edu/emt668/EMT668.Folders.../sine/assmt1.html" rel="nofollow">this</a> page:</p>

<blockquote>
  <p>The graphs of functions defined by y = sin x are called sine waves or sinusoidal waves. [...] This graph repeats every 6.28 units or 2 pi radians. It ranges from -1 to 1.</p>
</blockquote>

<p>So I came up with this:</p>

<pre><code>sin((hour/24)*pi)
</code></pre>

<p>You can integrate this in python like this:</p>

<pre><code>import math
import time
hour = int(time.strftime("%H"))
probability = math.sin(hour/24.0*math.pi)
print(probability)
</code></pre>

<p>Why? Because <code>sin(0*pi)</code> is 0. And <code>sin(0.5*pi)</code> is <code>1</code>. You want that at 12 hours the probability is the highest, so <code>1</code>. This value occurs on <code>sin(0.5*pi)</code>. On 24 hours, the value will be <code>sin(1*pi)</code>, which is <code>0</code> again.</p>

<p>Then I converted that value ranging from <code>0</code> to <code>1</code>, to <code>0</code> and <code>24</code>, by dividing the current hour by <code>24</code>.</p>

<p><a href="http://i.stack.imgur.com/Vgdvp.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/Vgdvp.jpg" alt="Half sine"></a></p>

python