Generate a normal distribution of dates within a range
<p>I have a date range - say between <code>1925-01-01</code> and <code>1992-01-01</code>. I'd like to generate a list of <code>x</code> dates between that range, and have those <code>x</code> dates generated follow a 'normal' (bell curve - see image) distribution.</p>

<p>There are many many answers on stackoverflow about doing this with integers (using <code>numpy</code>, <code>scipy</code>, etc), but I can't find a solid example with dates</p>

<p><a href="http://i.stack.imgur.com/dC2UB.png" rel="nofollow"><img src="http://i.stack.imgur.com/dC2UB.png" alt="enter image description here"></a></p>

<p>As per @sascha's comment, a conversion from the dates to a time value does the job:</p>

<pre><code>#!/usr/bin/env python3

import time
import numpy

_DATE_RANGE = ('1925-01-01', '1992-01-01')
_DATE_FORMAT = '%Y-%m-%d'
_EMPIRICAL_SCALE_RATIO = 0.15
_DISTRIBUTION_SIZE = 1000

def main():
    time_range = tuple(time.mktime(time.strptime(d, _DATE_FORMAT))
                       for d in _DATE_RANGE)
    distribution = numpy.random.normal(
        loc=(time_range[0] + time_range[1]) * 0.5,
        scale=(time_range[1] - time_range[0]) * _EMPIRICAL_SCALE_RATIO,
        size=_DISTRIBUTION_SIZE
    )
    date_range = tuple(time.strftime(_DATE_FORMAT, time.localtime(t))
                       for t in numpy.sort(distribution))
    print(date_range)

if __name__ == '__main__':
    main()
</code></pre>

<p>Note that instead of the <code>_EMPIRICAL_SCALE_RATIO</code>, you could (should?) use <code>scipy.stats.truncnorm</code> to generate a <a href="https://en.wikipedia.org/wiki/Truncated_normal_distribution" rel="nofollow">truncated normal distribution</a>.</p>

python date numpy gaussian normal-distribution