Python: generate “slightly” random values

Useful to generate plausible graphs, etc.

def slrgen(start=None,mindelta=0,maxdelta=5,minval=0,maxval=1000):
    if start is not None:
        s = int(start)
    else:
        s = randint(minval,maxval)
    while True:
        yield s
        s += choice([1,-1]) * randint(mindelta,maxdelta)
        s = min(maxval, max(minval, s))

Download source from github gist

comments powered by Disqus

Previous topic

Python: generate sine table

Next topic

Python: build extension module against library in non-standard path

This Page