import random random.seed(123) for i in range(10): print(random.random()) print(random.uniform(-1, 1))
import numpy numpy.random.seed(123) x = numpy.random.random(10) y = numpy.random.uniform(-1, 1, 10) print(x) print(y)
x = numpy.random.uniform(a, b, N)
x = numpy.random.uniform(-1, 1, N) y = numpy.random.uniform(-1, 1, N)
A simple application: 2-D uniform distribution. Here's an animated version: ran2d_anim.py
x = numpy.random.uniform(-1, 1, N) y = numpy.random.uniform(-1, 1, N) z = numpy.random.uniform(-1, 1, N)
dA = sinθ dθ dφ = -dμ dφwhere θ and φ are angular coordinates in spherical polar coordinates and μ = cosθ. Hence, to sample points on the sphere uniformly, we choose μ uniformly between -1 and 1 and φ uniformly between 0 and 2π:
costh = numpy.random.uniform(-1, 1, N) phi = 2*numpy.pi*numpy.random.uniform(0, 1, N) sinth = numpy.sqrt(numpy.maximum(0, 1-costh**2)) x = R*sinth*numpy.cos(phi) y = R*sinth*numpy.sin(phi) z = R*costhHere is a script that does this and displays the results.