Think Stats Chapter 4 Exercise 2 (a random distribution)
Exercise 4.2 The numbers generated by random.random are supposed to be uniform between 0 and 1; that is, every value in the range should have the same probability. Generate 1000 numbers from random.random and plot their PMF and CDF. Is the distribution uniform?
Problem: Do the numbers generated by the random function have a uniform distribution?
Code:
PMF:
import random
sample = [random.random() for i in range(1000)]
pmf = thinkstats2.Pmf(sample)
thinkplot.Pmf(pmf)
thinkplot.Config(xlabel = "random #", ylabel = 'probability')
CDF:
cdf = thinkstats2.Cdf(sample, label='random #')
thinkplot.Cdf(cdf)
thinkplot.Show(xlabel='random #', ylabel='CDF', loc = 'lower right')
Solution: Looking at the graph for the CDF of the random numbers generated, the distribution is very uniform, since the CDF is pretty much a straight line.