Lecture 09 Randomnumbers

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Practical Numerical

Training UKNum
Random numbers, Monte Carlo methods

H. Klahr
Max Planck Institute for Astronomy, Heidelberg

Program:
1) Random Number Generator
2) Transformation Method
3) Monte Carlo Integration
1.0 Random Numbers
Applications

‣Gambling
‣Physics Simulations
‣Monte Carlo Methods
‣Kryptography
‣...
Generating Random I

•Physical Methods (“real” random numbers)


•dice
‣Radioactive decay
(Time between two decay events)
‣Noise
(f.i. radio frequencies in the earth atmosphere)

•Advantage: “really” random


•Drawback: slow, difficult to control systematic
effects.
1955: A Million Random Digits

•Text
‣Text
text

A page chosen at random from the 1955 book A Million Random Digits.
Photo: Garry McLeod
Generating Random II

•Numerical Methods (“Pseudo” R.N.)


‣so called “Pseudo random number generators”
PRNG
‣Algorithms that generate numbers in which there is
apparently no pattern.
‣Start with “seed” to generate sequence of
numbers. Deterministic!
‣Same seed produces same sequence (important
for replicability).
‣After N-steps the sequence will start all over.
Numerical Pseudo-Random-
Numbers

•Advantage: simple and fast.


•Drawback: One has to test the sequence if it really
is “random”.
‣f.i. no correlation between pairs of numbers, no
preferred numbers, etc.
‣Statistical tests.
•Quality of RNG depends on application (f.i. length
of sequence).
Numerical RNG

•There are RNGs that fullfill all needs…


•... but also bad ones:
‣Historic bad example:
randu
‣IBM mainframes in the1960s. Widely used.
Result: Many wrong physical results. See
below…
1.1 Equal
distributed RNG
Why important
•“Uniform Deviates”

•Based on Uniform Deviates one can generate


all other distributions via Transformation
methods (f.i. normal distribution, Gaussian,
Exponential … etc.).

•Typical uniform rational numbers in the interval


0 to 1 (depending algorithm including or
excluding 0 and 1).
System-Supplied uniform PRNG

•Available in many languages and libraries.


‣Random class in Java, Apple CarbonLib, glibc (used
by gcc), Microsoft Visual/Quick C/C++, etc.
•Often low (or undefined) Quality.
•Often not portable. -> New platform?
•Better: portable PRNG (written in FORTRAN or
C).
ndom number generator.” That routine ty
Application
” and a calling sequence like
•Typical Calling Sequence (x: REAL, iseed: INTEGER)
x=ran(iseed) sets x to the nex

•First call
You‣Initialisation
initialize withiseed to aDepends
arbitrary seed. (usually)
on arbi
algorithm, sometimes neg. Odd number. For same
initializing value will typically return
seed same sequence. Returns new random number a
east a different
x and new value forsubsequence
iseed. of some on
•Next call:
izing
‣Use value
last of iseed
returned iseed. (!) will always return
Now our first, and perhaps most import
PRNG Algorithmen

•Middle square method


•Kongruence generator
‣linear
‣multiplicativ
•Mersenne-Twister
•...
Middle Square method:
•One of the first PRNG.
‣1946: John von Neumann.

•Only of historic importance:


(Short sequence, crashes at zero, etc.)
Kongruence generator

•Widely distributed
‣Not perfect but simple, quality depends on used
parameters, can be improved by some tricks to a
useful RNG.
•Very fast.
•Small demand in memory (cache).
Chapter 7. Random Numbers
Linear Kongruence generator

of•integers I 1 , I2 , Iof
Generates sequence 3 ,integers:
. . . , each
I1, I2, between
I3 ..., all 0a
between 0 and m-1 with m a “large” number.
urrence relation
•Recursive definition:
Ij+1 = aIj + c (mod m)
•m=Modulus (Integer, m>0)
• a=Multiplier/Faktor (Integer, 0< a < m)
modulus, and a and c are positive integers c
•c=Increment (Integer, 0<= c <m)
spectively. The recurrence (7.1.1) will even
obviously no greater than m. If m, a, and c a
Linear Kongruence generator II

•Repeats at least after m calls.


‣But for bad parameters even earlier!
•Good parameters: all possible numbers 0 to m-1
will occur once (Pseudo random permutation).
•seed then only determines, where the Sequence
is started!
Linear Kongruence generator III

•Equal distributed between 0 and 1 is then Ij+1/m.


‣All Numbers smaller than 1, but once in m calls gives
0.

•Requirements for m, a, and c (“Satz von Knuth”) for


maximising length of sequence (=m):
‣The Increment c up to modulus m has no common
dividend.
‣All prime factors m divide a-1.
‣If m can be divided by 4 then also a-1.

•Illustration: Mathematica "Linear Congruential Generators" from The Wolfram


Demonstrations Project http://demonstrations.wolfram.com/LinearCongruentialGenerators/ von Joe Bolte
Linear Kongruence generator IV

•Drawbacks
‣Sequential correlation (relation between RN
pairs), manifested in hyper planes(Satz von
Marsaglia):
Plotting a sequence of k numbers in a k dimensional space (k1,k2,k3),
then the points do not fill the volume, but lie on k-1 dimensional hyper
planes. Maximally m1/k Hyper planes, for “bad” values of m,a and c
even less.

‣Different likelihood for different bits within


numbers…
Hyper planes

(There planes k=3)


Miller
hey presenthave
[1] surveyed
an anecdotal a largeofnumber
sampling of of
a number random number
inadequate generath
generators
me Multiplicative Kongruence generator
used
into over
esent
the lastuse.
widespread
an anecdotal
30The
years or more.
historical Along
record with ifa not
is nothing good theoret
appalling.
re is good evidence,sampling of a number
both theoretical of inadequate
and empirical, that thegenerators
simple mult
widespread algorithm
congruential use. The historical record is nothing if not appalling
ood•Aka Park-Miller
evidence, PRNG. and empirical, that the simple mu
both theoretical
Ij+1 = aIj (mod m) (7.1
•Special case: c=0, then:
uential algorithm
good as any of the more general linear congruential generators that ha
Ij+1 = aIj (mod m) (7.
quation 7.1.1) — if the multiplier a and modulus m are chosen exquisite
•m and
Park andMiller
a have to bea chosen
propose “Minimal very carefully.
Standard” Parkbased
generator andon t
as any of the more general linear congruential generators that h
Miller suggest:
n 7.1.1) — if the multiplier a and modulus m are chosen exquisi
k and Miller 5propose a “Minimal
a = 7 = 16807 31 Standard” generator based on
m = 2 − 1 = 2147483647 (7.1

•Sobycalled
posed Lewis,“Minimal
Goodman,Standard”
and Miller MINSTD Generator.
in 1969, this generator has
nta•=Widely distributed,
7 = 16807
years m but
= 2should
5passed all new theoretical
− 1 =not be used for
2147483647
31 tests, and (perhaps more importantl
(7.
professional
mulated application.
a large amount of successful use. Park and Miller do not claim th
ator
by isLewis,
“perfect” (we will see
Goodman, below
and that it
Miller inis1969,
not), but only
this that it is ahas
generator go
Implementation of MINST

•Problem: Multiplications of a and Ij lead for some Ij


to Integer > 232 (4 byte=32 bit, standard Fortran
Integer, C long int)
‣Can be remedied with Schrage’s Algorithm for
approximative Factorisation of m.
•Numerical Recipes von Press et al., Cambridge
University Press.
http://www.nrbook.com/a/
MINSTD, 64 bit Integers

Press et al.
MINSTD: sample output
seed =1
Call No. x idum
1 7.82636926E-06 16807
2 0.13153780 282475249
3 0.75560534 1622650073
....
2147483645 0.65550071 1407677000
231-2 2147483646 4.65661287E-10
2147483647 7.82636926E-06
1
16807
231≈2 109

•MINSTD Problems:
•Low order serial correlations. Example:
Once in 106 calls, x < 10-6 ausgegeben (which is OK), Once in 106 calls,
x < 10-6 ausgegeben (which is OK), but next number is always < 0.0168,
which is not OK.
MINSTD with BD-shuffling
•Simple method after Bays and Durham, to prevent
2
low order serial correlations:
Chapter 7. Random Numbers

iy

RN: Ij, generatend at


1

http://www.nr.com or call 1-800-872-7423 (North America only), or sen


readable files (including this one) to any server computer, is strictly pro
Permission is granted for internet users to make one paper copy for th
Copyright (C) 1986-1992 by Cambridge University Press. Programs C
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE AR
position j in sequence,
iv1 OUTPUT

RAN 3 2 is not directly used but


randomly after, on
average, (j+32) calls
later.
iv32

ure 7.1.1. Shuffling procedure used in ran1 to break up sequential correlations in the Minimal
Press
ndard generator. et al.
Circled numbers indicate the sequence of events: On each call, the random number
iy is used to choose a random element in the array iv. That element becomes the output random
MINSTD mit BD-shuffling 7.1 Uniform Deviates 271

FUNCTION ran1(idum)
INTEGER idum,IA,IM,IQ,IR,NTAB,NDIV
REAL ran1,AM,EPS,RNMX
PARAMETER (IA=16807,IM=2147483647,AM=1./IM,IQ=127773,IR=2836,
* NTAB=32,NDIV=1+(IM-1)/NTAB,EPS=1.2e-7,RNMX=1.-EPS)
“Minimal” random number generator of Park and Miller with Bays-Durham shuffle and
added safeguards. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of
the endpoint values). Call with idum a negative integer to initialize; thereafter, do not

http://www.nr.com or call 1-800-872-7423 (North America only), or send em


readable files (including this one) to any server computer, is strictly prohibite
Permission is granted for internet users to make one paper copy for their ow
Copyright (C) 1986-1992 by Cambridge University Press. Programs Copyrig
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF
alter idum between successive deviates in a sequence. RNMX should approximate the largest
floating value that is less than 1.
INTEGER j,k,iv(NTAB),iy
SAVE iv,iy
DATA iv /NTAB*0/, iy /0/
if (idum.le.0.or.iy.eq.0) then Initialize.
idum=max(-idum,1) Be sure to prevent idum = 0.
do 11 j=NTAB+8,1,-1 Load the shuffle table (after 8 warm-ups).
k=idum/IQ
idum=IA*(idum-k*IQ)-IR*k
if (idum.lt.0) idum=idum+IM
if (j.le.NTAB) iv(j)=idum
enddo 11
iy=iv(1)
endif
k=idum/IQ Start here when not initializing.
idum=IA*(idum-k*IQ)-IR*k Compute idum=mod(IA*idum,IM) without overflows by
if (idum.lt.0) idum=idum+IM Schrage’s method.
j=1+iy/NDIV Will be in the range 1:NTAB.
iy=iv(j) Output previously stored value and refill the shuffle ta-
iv(j)=idum ble.
ran1=min(AM*iy,RNMX) Because users don’t expect endpoint values.
return

Press et al.
END
MINSTD with BD-shuffling

• Perfect up to m/20 (about: 10 8 calls).

•Longer Sequences need PRNGs that combine


multiple Sequences with different length. Then even
a sequence of 2.3 x 1018
randu
•Multiplicativ Kongruence-generator. Recursion:
Vj+1 = 65539 · Vj mod 2 31
With V0 odd
•Range [1:231-1]
•Rational number in range ]0,1[
Vj
Xj = 31
2

•Chosen parameters (with 32 bit Integer) thus mod


231 and also multiplication with 65539=216+3 is fast
from the hardware side.
randu - continued II

•Problem: follow 3 steps (mod 231)


16 16 2
xk+2 = (2 + 3)xk+1 = (2 + 3) xk

xk+2 = (232 + 6 · 216 + 9)xk = [6 · (216 + 3) 9]xk

As 232 mod 231 = 0

•Then
xk+2 = 6xk+1 9xk
Oops!!!
2. Non equal RNG:
Transformation Method
Statistical Distribution
bility distributions
or •
population
X is givensynthesis calculations
via (cumulative is the generation
distribution of initial cond
function,
robability
CDF) F(x)distributions, as explained in the introduction to Paper I
beschrieben.
a short overview on probability distributions in general.
•Then F(x) is probability, that X has value between
iate X is
-∞ and x:described with the cumulative distribution function (CD
obability that the random deviate X takes a values between ⇤ a

F (x) = P (X ⇥ x) for ⇤<x<⇤

•Requirement
lfill for CDF: namely (1) F ( ⇤) = 0, (2) F (⇤
an number of requirements,
•F(-∞)=0,
notonically F(∞)=1 and (4) F (x) must rightwards continuous
increasing,
•Monotonically raising
•Continuous
y that a continuous random deviate takes a given value xi is zero. T
Statistical Distribution
F (x) = P (X ⇥ x) for ⇤<x<⇤

l an number of requirements, namely (1) F ( ⇤) = 0, (2) F (


tonically increasing, and (4) F (x) must rightwards continuo
•Probably for X to be exactly xi is 0.
• probability
hat a continuousdensity
randomfunction PDF: af(x)
deviate takes means
given valuetherefor
xi is zero.
itythat X is in(PDF)
function the infinitesimal interval
f (x) is used whichofstates
dx: f(x)dx.
that the proba
imal interval dx is given as f (x)dx. The PDF is related to th
x
F (x) = P (X ⇥ x) = f (x)dx,

•f(x) thus is derivative of F(x).
erivative of F (x). The geometrical interpretation is of course
der the curve given by f (x) to the left of x. Similarly is the
Popula
terval [a,
Statistical
nterval given as
[a, b] given Distribution
b] as
⇧⇧b
nterval [a, b] Pgiven
(a ⇥ as
X ⇥ b) =
b
f (x)dx.
P (a
•Probability for X in⇥[a,b]
X⇥ is b) = f (x)dx.
⇧aba
ive function P
of(a ⇥
the X ⇥ b)
random = f
deviate(x)dx.
X, then it is itself a r
tive function of the random deviate
a X, then it is itself a
dom
ndom deviate
deviate the
the expectation
expectation value
value of g(X),
of g(X), E(g(x))
E(g(x))is th
is
• function of the random
If
tive g(x) a bijective Function

of X, then it is also a random
deviate X, then it is itself a ra
distribution. Expected value ⇧E⇥of g(X), E(g(x)) is then
ndom deviate the expectation value of g(X), E(g(x)) is th

• E(g(X)) =
E(g(X)) =⇧ ⇥ g(x)f
g(x)f (x)dx.
(x)dx.


E(g(X)) = g(x)f (x)dx.
the
the expectation
expectation value of X ⇥itself, which is
•Special case if X value of X itself,
is the expected valuewhich is
⇧ ⇥
the expectation value . of X itself,
⇧ ⇥ which is
E(X) =. µx = ⇧ xf (x)dx.
E(X) = µx = ⇥ ⇥
xf (x)dx.
.
E(X) = µx = xf (x)dx.

once the synthetic
1 Generic F (x) = In bthis
planets were calculated).
distributions a if a ⇥
program, xa ⇥ b

probability distributions were implemented. 1 generates if xthe>initial
Equal distribution in [a,b]
program exostat which was written for this research project b. condi-
for the populations synthesis calculations (it is also used for the statistical comparison
s CDF is obtained by setting dF = f (x)dx = k1 dx and deter
the observational data once the synthetic planets were calculated). In this program, a
ber of important generic probability distributions were implemented.
F (a)
•Then
ction has=CDF
a0CDF
andandFPDF (b) = 1. The PDF is obtained as the derivat
form deviate ⇥in [a, b]
⌅ 0 if x < a 1
if a ⇥ x ⇥ b
simplest probability function has a CDF
F (x) = x a
if a ⇥ x ⇥ b f (x) = b a
(3.7)
⇤ b a ⇥
0 31 if 0
Probability distributions otherwise
1 if x > b. ⌅ x < a
x a
F (x) = b a if a ⇥ x1⇥ b 1 (3.7)
1 ⇤
se distributions are shown 1 in
ting dF = f (x)dx = k1 dx and determining k1 by the conditions 0.8
0.8
iffig. 3.1.
x > 0.8
b.

The
The mean
CDF PDF is0.6is
is obtained ofsetting
obtained
by course
asdFthe=simply
fderivative,
(x)dx = k1 dx 0.6
and determining k1 by the conditions
0.6

F (a) = 0 and 0.4F (b) = 1. The PDF is obtained as the 0.4 derivative, 0.4
0.2 1
if a ⇥ x ⇥ b ⇧ b
f (x) = 0 b a 1
if a ⇥
0.2
x ⇥ b 1 (3.8)
0.2
a + b
.5 1 1.5 2 0 !0.5 otherwise
!1 0 f (x)1 = 1.5 E(X)
0.5
0
b a
2 = µ 0
= xdx
otherwisex!1 !0.5b 0 a0.5 1 1.5 2
= 0 (3.8)

PDF (left) and CDF (right) for a standard uniform deviate. a 2


!1 !0.5 0

ne in fig. 3.1. Figure 3.1: PDF (left) and CDF (right) for a standard un

2

a)2
.
distributions
Simple
This distribution is
are shown
case:
The mean is of course simply
mply implemented as
in fig. 3.1.
(a+b)/2,
No. 1 in
but then we can test the general Eq.:
exostat. (b a)2
niform deviate in the unit interval (i.e. between 0.0 and 1.0) which and the variance 2
x= 12 . This distribution is implemented as
er generators. Called the standard uniform deviate, it has a PDF ⇧ b A special case is the uniform deviate in the unit interval (i.e. b
n the unit interval, and 0 otherwise. In ⇧the practical application for1 a+
is given by b number generators. Called the standard unif
random
1 from
wants to calculate random deviates E(X) b
some given x =a + b
= µdistribution, equal=
xdx (3.9) In t
1, i.e. f (x)dx = dx in the unit interval, and 0 otherwise.
r generator=
E(X) µx = which gives a standard
at disposition xdx =deviate.
uniform b a a a computer 2program, one wants to calculate (3.9)random deviates from
Equal distribution in Intervall [0,1]

•PRNG special case Equal distribution aka


standard uniform deviate (SUD).
•Simple PDF =1,i.e. f(x)dx=dx inside interval
otherwise 0.
•Very useful to generate arbitrary distributions
with transformation method.
toto
ble useusethe thetransformation
transformation method
method to to draw
draw r
way Transformation
e way explained
explainedininPress methode
Press et
et al. I
al. (1992).
(1992).
er
uter program
programcan cangenerate
generate aa standard uniform
standard uniform
m•Ifdeviate
dom followsYaY,SUD.
X,deviate ,with
with samples
Whatsamples thatsamples
is then Y,yywhich followyiso
follow s
following f(y)
f (y),wewecan
thisf (y), ? canuse
usethe
the fundamental
fundamental law of
is law of
• Following fundamental law of probabilities:
nvenasas
|f (y)dy| = |f (x)dx|
|f (y)dy| = |f (x)dx|
Thus (as f(x)=1 for SUD)
dx dx
f (y) = f (x) dx = dx
f (y) = f (x) dy = dy
dy dy
|f (y)dy| = |f (x)dx|
|f (y)dy| = |f (x)dx|
Transformation-methode II
dxdx dx
dx
f (y)f=
(y)f=(x)
f (x) =
=
dydy dy
dy

is •simple
Solution if D.E. isequation
differential x=F(y), with F(y)
is just x= theF integral of f(y).
(y), where F (y)
eherefore,
differential equation is just x = F (y), where
the transformation of a uniform deviate x to y distr
F
e, the transformation of a uniform deviate x to y
• Transformation of SUD from X to Y with f(y) :
y(x) = F 1
(x),

s the inverse y(x) = F


function of F . (x),
1
The transformation method ca
to the indefinite integral of f can be calculated. An example h
verse
With Ffunction of function
-1 the inverse F . Theoftransformation
F. meth
Sotoone
ndefinite
ant has toan
beuniform
integral
generate able
of fto can
integrate f!in the interval
beYcalculated.
deviate An exam
[a,b]. F
ly understand that we have to calculate y = x(b a) + a when
Example

•Equal distribution Y in Intervall [a,b], and SUD X.


•Suggestion: y=x(b-a)+a.
•But use Transformations-method:
•Be x=F(y)=(y-a)/(b-a), and solve for y auf (reverse ->
F-1), thus y(x)=x(b-a)+a
e a. We can again calculate with the transformation method how we
Example 2: Uniform in lg
y = e x(ln(b)
able distributed according to this ln(a))+ln(a)
distribution if we have an standard
tting x = F (y) and solving for y we find
.
• Y be uniform in log10 within [a,b].
y = ex(ln(b) ln(a))+ln(a)
to the base 10 is completely parallel, with th thus lg(x) is
uniform in [lg(a),lg(b)]
e logarithm to the
⌃base 10 is completely parallel, with the CDF given a
0 0

if ax < a
if x <
log(x)
log(x) log(a)
log(a)
F (x) = F (x) =

log(b)
log(b)
log(a) if
if a ⇥ x ⇥ b
log(a)if x > b. a ⇥ x ⇥ b
1
⌥ 1 if x > b.
amples from this distribution one uses the equation
• Transformation -10method (x=F(y) ->
m this distribution one uses the equation
y = x(log(b) log(a))+log(a)
. inverse),

iformly distributed in the unit interval, then y is distributed log unifo


y = 10
means that log(y) is x(log(b)
uniformly log(a))+log(a)
distributed in [log(a),.log(b)]. The dist
(x) is implemented as No. 2 in exostat.
Generalisation

•Can be used in multiple dimensions. (Aka Box


Muller Transformation)
•Not all distribution functions can be integrated.
Then use the Rejection method instead.
•See discussion in Numerical Recipes: Press et al.
3. Monte Carlo
Methods
Idea Monte Carlo Integration
7.6 Simple Monte Carlo Integration
•Calculate complex Integrals of Function f in
multidimensional Volume V.
se that we pick7.6NSimple
random points, uniformly distributed in a multid
• Monte Carlo Integration
Distribute N RND x1,..,xN uniform in V.
me V . Call them x1 , . . . , xN . Then the basic theorem of Monte
305

• MC
estimatestheorem
the says
integral of we
a can
function then
f overestimate
the the
multidimensional vo
Integral as:
Suppose that we pick N random points, uniformly distributed in a multidimen-

nal volume V . Call them x1 , . . . , xN . Then the basic theorem of Monte Carlo

egration estimates the integral of a function f over2the 2
multidimensional volume,
⇧f ⌥ ⇥ ⇧f⌥
f dV ⌃ V ⇧f⌥ ± V ⇧ (
⇥ 2 N
⇧f ⌥ ⇥ ⇧f⌥ 2
f dV ⌃ V ⇧f⌥ ± V (7.6.1)
gle brackets denote taking the arithmeticNmean over the N sample p
• With <> the arithmetic mean over N sample points.
re the angle brackets denote taking the arithmetic mean over the N sample points,
⌅N ⌅N
1 N
⌃ 2⌥ 1⌅
N 2
⇧f⌥ ⌅
1 f(xi ) ⌃ f2 ⌥ 1 2 f (xi ) (
⇧f⌥N f(xi ) f N f (xi ) (7.6.2)
N
i=1 i=1
N i=1
i=1
Monte Carlo Integration II

•In general difficult to have points evenly in V , f.i.


complex shape of V.
•Define minimal Volume W, that contains V and then
set f only for points inside V and =0 for outside V
(rejection method). 306 Chapter 7. Random Numbers

area A

∫fdx

Press et al.
Example ∫fdx

•Center of mass for arbitrary shaped body:


Figure 7.6.1. Monte Carlo integration. Random points are chosen within the area A. The integral of the
function f is estimated as the area of A multiplied by the fraction of random points that fall below the
curve f . Refinements on this procedure can improve the accuracy of the method; see text.

2
Torus, with certain cuts…

x
Difficult to integrate
0 1 2 4
analytically.

For MC simple.
Z
1
Press et al.
~rs = ⇢(~r)~rdV
M
, (see
butbuta§7.8),
a worked
worked example will show the underlying simplicity
but a worked example will show the underlying simplicity of the method.m
example will show the underlying simplicity of theof
hat
at weExample
we
Suppose want
want to
that we to find
find
want
II
the
to the
find theweight
weight and
weight and
and thethe
the position
position
position of of of
thethe the
center
center center
of
of mass
object of complicated shape, namely the intersection of a torus with the edge of a
anof
ofmas
complicated
omplicated shape,
shape, namely
namely thethe intersection
intersection of aoftorus
a torus
withwith
the ed th
large box. In particular let the object be defined by the three simultaneous conditions
nInparticular
particular letlet
thethe
object be
object defined
⇤ be definedby 2the three
by the simultaneous
three simultaneou con
•Body defined as⇤z2 +⇤ x2 + y2 ⇥ 3 2 ⇤ 1 (7.6.3)
2
2 2 2 +2 y2 ⇥23
z z+ + x x + y ⇥ 3⇤
(torus centered on the origin with major radius = 4, minor 1⇤radius
1 = 2)
x ⌅ 1radius
y ⌅=⇥34, minor radius = 2)
(7.6.4)
(Torus
ered on
tered onouter
the radius with
origin = 4, inner
with
the origin majorradius
major =3)=and
radius planes radius = 2)
4, minor
(two faces of the box, see Figure 7.6.2). Suppose for the moment that the object
has a constant density ⇧. x ⌅ x⌅11 y ⌅ ⇥3
y ⌅ ⇥3
We want to estimate the following integrals over the interior of the complicated
of• Find
the
object: the
box, Volume
see Figure for the
7.6.2). Integrals
Suppose
s of ⇥the box, see ⇥Figure 7.6.2). ⇥Suppose for the for the moment
⇥ moment tha
that the
tant density ⇧.
nstant ⇧density
dx dy dz ⇧. x⇧ dx dy dz y⇧ dx dy dz z⇧ dx dy dz
ant to estimate the following integrals over the interior of the comp
want to estimate the following integrals over the interior of(7.6.5)
the c
The coordinates of the center of mass will be the ratio of the latter three integrals
• Assume
(linear moments)⇥ constant

density
to the first one ⇥ρ.
(the weight).



In the following
x dy dz x⇧fragment,
dx dy dz the region V , y⇧
enclosing
dx dythedz
piece-of-torusz⇧
W dx
, is the
dy
Example III 306 Chapter 7. Random Numbers

area A

•minimal Volume W, in which we are sampling points


uniform in a box of 1 to 4 in x, -3 to 4 in y, and -1 to 1 ∫fdx

in z. Figure 7.6.1. Monte Carlo integration. Random points are chosen within the area A. The integral of the
function f is estimated as the area of A multiplied by the fraction of random points that fall below the
curve f . Refinements on this procedure can improve the accuracy of the method; see text.

x
0 1 2 4
Z
1
~rs = ⇢(~r)~rdV
M

•Results for n=1000000


w,dw 22.107918 2.09707543E-02 (Masse des Körpers; Umgebender Quader: 3x7x2=42)
x,dx 53.275669 5.50482012E-02
y,dy 3.4985492 5.61315641E-02
z,dz -1.39091080E-02 1.53482482E-02
0 1
2.4098
•Center of mass ~rs = @ 0.1582 A
0.0006

At least plausible (z-component =0 … Symmetry!)


• Exercise 1, 6 points: The infamous randu.
Write a program code with the (not-so) random number generator randu following
the recurrence:
⇣ ⌘
31
Ij+1 = (65539 Ij ) mod 2

(see Lecture for details). Then to obtain a random number xi drawn from the interval
(0, 1) use the normalisation xi = ui /231 . Create some 100 000 random number,
starting with an initial seed u0 = 1 and plot the consecutive triples (xi , xi+1 , xi+2 )
in a 3-dimensional plot, e.g., using the splot command from gnuplot. Count the
number of 2D planes by viewing the data in di↵erent projections. What is the
number of planes for randu?
• Exercise 2, 8 points: Transformation method.
Write a program code to generate random numbers with an exponential probability
distribution function (PDF) ⇢(y) = e y in the interval ymin = 0 to ymax = 5 using
the transformation:

y= ln (1 x) , x = e y.

Use an random number generator with uniform PDF of your choice or the one
o↵ered in the Lecture. Show that your resulting distribution of random numbers
indeed follows an exponential one.
• Exercise 3, 6 points: Monte-Carlo Integration.
Approximate the value of ⇡ using the Monte-Carlo technique by integrating the area
of a square with side length a and a circle of radius 1/2a. Use the equation:

Ac Nc
⇡=4 ⇡4 ,
As Ns

where Ac and As is the area of the square and the circle, respectively. How does the
precision of the result scale with the number of points used in the integration?

You might also like