Gauss test.py

From Werner KRAUTH

(Difference between revisions)
Jump to: navigation, search
Revision as of 21:18, 22 September 2015
Werner (Talk | contribs)

← Previous diff
Current revision
Werner (Talk | contribs)

Line 1: Line 1:
 +This page presents the program gauss_test.py, a direct-sampling algorithm for two independent Gaussian random numbers. This algorithm is used to illustrate the concept of "sample transformation"
 +
 +__FORCETOC__
 +=Description=
 +
 +=Program=
import random, math import random, math
Line 14: Line 20:
[x, y] = gauss_test(1.0) [x, y] = gauss_test(1.0)
print x, y print x, y
 +=Version=
 +See history for version information.
 +
 +[[Category:Python]] [[Category:Honnef_2015]] [[Category:MOOC_SMAC]]

Current revision

This page presents the program gauss_test.py, a direct-sampling algorithm for two independent Gaussian random numbers. This algorithm is used to illustrate the concept of "sample transformation"


Contents

Description

Program

import random, math

def gauss_test(sigma):
    phi = random.uniform(0.0, 2.0 * math.pi)
    Upsilon = random.uniform(0.0, 1.0)
    Psi = - math.log(Upsilon)
    r = sigma * math.sqrt(2.0 * Psi)
    x = r * math.cos(phi)
    y = r * math.sin(phi)
    return [x, y]

nsamples = 50
for sample in range(nsamples):
    [x, y] = gauss_test(1.0)
    print x, y

Version

See history for version information.

Personal tools