jeudi 17 décembre 2015

Python: rounding error distorts uniform distribution

I need to sample 10 uniformly distributed random numbers between 0 and 1. So I thought the following code in python would do this:

positions = []
for dummy_i in range(1000000):
    positions.append(round(random.random(),1))

However when putting the result into a histogram the result looks as follows:

Frequency of numbers rounded to 1 decimal place

So the rounding seems to destroy the uniform distribution generated by random.random(). I wonder what causes this and how to prevent this from happening. Thanks for your help!




Aucun commentaire:

Enregistrer un commentaire