Download presentation
Presentation is loading. Please wait.
Published byBuddy Ford Modified over 9 years ago
1
TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th, 2012 CECAM school on Numerical simulations F. Becca & G. Lattanzi SISSA
2
Random numbers generators The programming language of your choice contains a built-in function that generates random numbers: Fortran: RAND C++: rand Perl: rand() Python: random There are also alternatives, based on different and more sophisticated algorithms, e.g. those proposed by Numerical Recipes http://www.nr.com/
3
Exercises with random numbers (1) 1.Casuality test: consider the function, being r a random variable uniformly distributed between 0 and 1: In the large N limit: Verify that your favourite built-in random number generator passes the confidence test at 5%, i.e. that 95% of the realizations fall in the interval (μ-2σ,μ+2σ). Plot the obtained values of ψ for N=10, 20, 50, 100.
4
Modules To see the list of available modules: module avail To load a specific module: module load name_module LOCATED AT id001719 CALLED random.c Real(8) drand1 Iseed=142527 Call rand_init(iseed) R=drand1()
5
Exercises with random numbers (2) 2. Random integration. Consider the following integral: We will evaluate the integral exploiting random numbers: we generate N numbers x i in the interval [0,1] and calculate: Verify that the average value is, as expected, π/4. What about the variance? What is its expected value?
6
Exercises with random numbers (3) 3. Rejection method. We want to obtain a sequence of random numbers distributed according to a probability distribution ρ defined in [a,b] with a maximum value ρ ΜΑΧ. Method: I.We extract two series of random numbers uniformly distributed between 0 and 1: we call them η i and θ i. II.We use the η i ’s to obtain a sequence of random numbers x i uniformly distributed between a and b: III.We use the θ i ’s to generate random numbers y i uniformly distributed between 0 and ρ ΜΑΧ : IV.We accept the (x i,y i ) if and only if:
7
Exercises with random numbers Algorithm: 1.Extract η i 2.Obtain x i =(b-a)ηi+a 3.Extract θ i 4.Obtain y i =θ i *ρ ΜΑΧ 5.Check: y i ≤ ρ(x i )? If yes, accept (x i, y i ). If not, reject it. Exercise 3. Use the above specified algorithm to generate random numbers distributed between -1 and 1 with probability density: Having divided the interval [-1,1] in bins, plot the histogram of the values generated with this procedure. Plot also in the plane (x,y) the points obtained.
8
Exercises with random numbers Justification of the algorithm The probability to obtain the sequence of numbers y i is the product of the probability to extract x i with the given uniform probability distribution: And the acceptance probability This function is exactly ρ(x), apart from a normalization factor:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.