Direct sphere.py

From Werner KRAUTH

Jump to: navigation, search

This page presents the program direct_sphere.py, a direct-sampling algorithm for uniform random points inside the three-dimensional unit sphere.


Contents

Description

Program

import random, math

nsamples = 100
for sample in xrange(nsamples):
    x, y, z = (random.gauss(0.0, 1.0),
               random.gauss(0.0, 1.0),
               random.gauss(0.0, 1.0))
    length = random.uniform(0.0, 1.0) ** (1.0 / 3.0) \
                    / math.sqrt(x ** 2 + y ** 2 + z ** 2)
    print x * length, y * length, z * length

Version

See history for version information.

Personal tools