Q6.6.2Using np.random.randint to sample from a set of evenly-spaced reals
In Example E6.18 we used random.random_integers to sample from the uniform distribution on the floating point numbers \([\frac{1}{2}, \frac{3}{2}, \frac{5}{2}, \frac{7}{2}]\). How can you do the same using the np.random.randint instead?
Show solution
The function np.random.randint samples uniformly from the half-open interval, [low, high), so to get the equivalent behaviour to np.random.random_integer in Example E6.18 we need:
In [x]: a, b, n = 0.5, 3.5, 4
In [x]: a + (b-a) * (np.random.randint(1, n+1, size=10) - 1) / (n-1)
Out[x]: array([ 0.5, 1.5, 0.5, 3.5, 1.5, 3.5, 2.5, 0.5, 1.5, 1.5])