RANDOM NUMBERS GENERATION

Similar documents
RANDOM NUMBERS GENERATION

You ve already read basics of simulation now I will be taking up method of simulation, that is Random Number Generation

Scientific Computing: An Introductory Survey

Linear Congruential Number Generators. A useful, if not important, ability of modern computers is random number

Decision Support and Intelligent Systems. Monte Carlo Simulation

UNIT 9A Randomness in Computation: Random Number Generators

Computational Methods. Randomness and Monte Carlo Methods

Proposed Pseudorandom Number Generator

is bad when combined with a LCG with a small multiplier. In section 2 this observation is examined. Section 3 gives portable implementations for LCGs

b) Describe a procedure visitors should follow to find a free parking space, when the space they are assigned is occupied.

Simulation. Chapter Copyright 2010 Pearson Education, Inc. Publishing as Prentice Hall

Lecture 2: Introduction to Numerical Simulation

Reproducibility in Stochastic Simulation

UNIT 9A Randomness in Computation: Random Number Generators Principles of Computing, Carnegie Mellon University - CORTINA

Physics 736. Experimental Methods in Nuclear-, Particle-, and Astrophysics. - Statistical Methods -

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

What We ll Do... Random

Physics 736. Experimental Methods in Nuclear-, Particle-, and Astrophysics. - Statistics and Error Analysis -

Statistical Computing

YEAR 7 SCHEME OF WORK - EXTENSION

Graphical Analysis of Data using Microsoft Excel [2016 Version]

Physics 736. Experimental Methods in Nuclear-, Particle-, and Astrophysics. Lecture 14

CS 450 Numerical Analysis. Chapter 7: Interpolation

LASER s Level 2 Maths Course - Summary

Correlation of Ontario Mathematics 2005 Curriculum to. Addison Wesley Mathematics Makes Sense

Chapter 4: (0,1) Random Number Generation

Chapter 6 Random Number Generation

GENERATION OF RANDOM NUMBERS BY COMPUTER

Network Security. Random Number Generation. Chapter 6. Network Security (WS 2003): 06 Random Number Generation 1 Dr.-Ing G.

TABLES AND HASHING. Chapter 13

Random Number Generator of STMC and simple Monte Carlo Integration

Monte Carlo Integration and Random Numbers

Using Excel for Graphical Analysis of Data

What is the Monte Carlo Method?

Blum-Blum-Shub cryptosystem and generator. Blum-Blum-Shub cryptosystem and generator

Functional Programming. Big Picture. Design of Programming Languages

Ph3 Mathematica Homework: Week 8

Ch 3.4 The Integers and Division

Tutorial: Random Number Generation

CHAPTER 2 Data Representation in Computer Systems

Manual_implementation_of_the_Mersenne_twister_PseudoRandom_N

Introduction to Matlab

Lecture 12 Notes Hash Tables

Monte Carlo Techniques. Professor Stephen Sekula Guest Lecture PHY 4321/7305 Sep. 3, 2014

Probabilistic (Randomized) algorithms

VON NEUMANN ARCHITECTURE. VON NEUMANN ARCHITECTURE IB DP Computer science Standard Level ICS3U

A simple OMNeT++ queuing experiment using different random number generators

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

4.1 Performance. Running Time. The Challenge. Scientific Method. Reasons to Analyze Algorithms. Algorithmic Successes

Chapter 2 Random Number Generators

SECTION 1.3: BASIC GRAPHS and SYMMETRY

SOME NOTES ON MULTIPLICATIVE CONGRUENTIAL RANDOM NUMBER GENERATORS WITH MERSENNE PRIME MODULUS Dr. James Harris*

Halftoning and quasi-monte Carlo

Institute for Statics und Dynamics of Structures Fuzzy Time Series

Equations and Functions, Variables and Expressions

Random Numbers Random Walk

CHAPTER 2 Data Representation in Computer Systems

Mathematical Data Operators

CS61B Lecture #32. Last modified: Sun Nov 5 19:32: CS61B: Lecture #32 1

Macmillan Mathematics Unit (Level, Unit, Page, Exercise number) Not covered Rounding large numbers is covered in Level 4, Unit 1.

MATLAB Part 1. Introduction

Downloaded from

CIS 194: Homework 6. Due Wednesday, 4 March. Fibonacci numbers. It s all about being lazy.

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

True Random Number Generator using Solar Output Characteristics

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Counting shapes 1.4.6

Secure understanding of multiplication of whole numbers by 10, 100 or 1000.

Probability and Statistics for Final Year Engineering Students

LAB 2: DATA FILTERING AND NOISE REDUCTION

UNIT 9A Randomness in Computa5on: Random Number Generators. Randomness in Compu5ng

Random Numbers and Monte Carlo Methods

Lecture 12 Hash Tables

George Landon Chao Shen Chengdong Li

number Understand the equivalence between recurring decimals and fractions

Application of Two-dimensional Periodic Cellular Automata in Image Processing

Laboratory Exercise #5

Hash Functions. Kuan-Yu Chen ( 陳冠宇 ) TR-212, NTUST

Floating-Point Numbers in Digital Computers

Floating-Point Numbers in Digital Computers

Stream Ciphers. Çetin Kaya Koç Winter / 13

Gauss for Econometrics: Simulation

Midterm Exam 2B Answer key

D-Optimal Designs. Chapter 888. Introduction. D-Optimal Design Overview

142

Random Number Generators. Summer Internship Project Report submitted to Institute for Development and. Research in Banking Technology (IDRBT)

Computational modeling

Mathematics - LV 5 (with QuickTables)

Short-Cut MCMC: An Alternative to Adaptation

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems

PSEUDORANDOM numbers are very important in practice

Probability and Random Processes ECS 315

Random Number Generators

CSCE GPU PROJECT PRESENTATION SCALABLE PARALLEL RANDOM NUMBER GENERATION

Cambridge Primary Mathematics Stage 5

C Functions. 5.2 Program Modules in C

7 th GRADE PLANNER Mathematics. Lesson Plan # QTR. 3 QTR. 1 QTR. 2 QTR 4. Objective

Consider the actions taken on positive integers when considering decimal values shown in Table 1 where the division discards the remainder.

WHOLE NUMBER AND DECIMAL OPERATIONS

The Plan: Basic statistics: Random and pseudorandom numbers and their generation: Chapter 16.

Transcription:

Chapter 4 RANDOM NUMBERS GENERATION M. Ragheb 10/2/2015 4.1. INTRODUCTION The use and generation of random numbers uniformly distributed over the unit interval: [0, 1] is a unique feature of the Monte Carlo method. These are used as building blocks for constructing and sampling probability density functions that represent any of the processes or phenomena that are under investigation. Meaningful sequences of random numbers must be generated for valid results to be generated. Otherwise the infamous gigo (garbage in, garbage out) adage of numerical computations would apply. A user of Monte Carlo is well advised to the check the validity, including length and period and randomness of his sequence of random numbers before embarking on a major simulation. Random numbers can be obtained or generated in many different ways. The last digits in phone numbers; but not the first numbers, in a phone directory can be used as random numbers. Tables of random numbers that were statistically tested for randomness have been published, just like trigonometric functions tables. The white noise from electronic equipment and the decay of radioactive isotopes, being random phenomena have been used to generate random numbers. Spinning a roulette wheel with its perimeter divided into ten sections can generate a random sequence of digits from 0 to 9. This particular way of generating random digits sequences suggested the name of Monte Carlo, alluding to the famous gambling casino at Monte Carlo in the municipality of Monaco, by Nice in Southern France. 4.2 THE MID-SQUARE METHOD Computer usage depends on the mathematical generation of sequences of random numbers that are long enough, or having a long period, so that they do not repeat themselves in a given simulation. If the sequence starts repeating itself, the sampled points would be repetitions of previous points. The repeated samples are useless and would not yield any new information. In the mid-square method, due to John von Neumann, an initial number n0 is raised to its second power. Let us consider a number of four significant digits. Then the four middle digits of the ensuing number are kept to constitute the next number in the sequence, the two digits to the left and the two digits to the right are discarded, and the process repeated. We show an example of such a generated sequence as follows: n0 = 0.986 n0 2 =0.953536

n1 =0.5353 n1 2 = 0.28654609 n2 = 0.6546 n2 2 = 0.428501 n3 = 0.8501 n3 2 = 0.226001 n4 = 0.260.... This method is marginally satisfactory, and the multiplicative congruential method has universally replaced it. 4.3 THE MULTIPLICATIVE CONGRUENTIAL METHOD This method is the best studied and most widely used method for random sequences generation. It generates pseudo-random sequences of random numbers uniformly distributed over the unit interval. It depends on the use of the recursive relation: x { ax c}modulo( m) (1) i i 1 The notation modulo or sometimes mod signifies that xi is the remainder when {axi-1 + c} is divided into m. Here m is a large integer determined by the design of the computer, usually a large power of 2 or 10, and a, c, and xi are integers between 0 and (m-1). The initial value x0 is designated as the seed of the sequence. In many applications of the method the constant c is taken as zero, yielding a simpler form of Eqn. 1: x { ax }modulo( m) (2) i i 1 The value of m is normally taken as the largest number that can be generated on a computer, which depends on the number of bits used in its processor and its data busses. In this case for a design of n bits: m = 2 n 1 (3)

For instance, for a hypothetical n = 4 bits machine, the largest number that can be generated would be according to Eqn. 3: This number expressed in the binary notation is: The numbers: m = 2 4 1 = 1 =15. m = 1111 = 1x2 0 + 1x2 1 + 1x2 2 +1x2 3 = 1x1 + 1x2 + 1x4 + 1x8 = 1 + 2 + 4 + 8 = 15 x i i, i 1,2,3,...( m 1), (4) m and not just xi, are taken as the pseudorandom sequence over the unit interval. The advantages of using pseudorandom sequences are that a calculation can always be repeated, starting from the same seed number, for comparison and testing purposes of programs, a few simple operations are needed, and the program uses a few memory positions. The only disadvantage is that the sequence must satisfy certain conditions for not repeating itself after a long length or period. As an example of a random sequence using Eqn. 3: Let: Seed x0 = 2 m = 2 4 = c = 1 a = 3 2 Then: x0 = 2 0 0. 1250 x1 = {3x 2 +1} mod = mod = 1 0. 435

6 x2 = {3x +1} mod = 22 mod = 6 2 0. 350 3 x3 = {3x 6 +1} mod = 19 mod = 3 3 0. 185 10 x4 = {3x 3+1} mod = 10 mod = 10 4 0. 6250 15 x5 = {3x10+1} mod = 31 mod = 15 5 0. 935 14 x6 = {3x15+1} mod = 46 mod = 14 6 0. 850 11 x = {3x14+1} mod = 43 mod = 11 0. 685 2 x8 = {3x11+1} mod = 34 mod = 2 8 0. 1250 x9 = {3x 2 +1} mod = mod = 9 0. 435 We notice that the sequence obtained for the xi s is: 2,, 6, 3, 10, 15, 14, 11, 2,,.. so that the sequence started repeating itself with a period length of 8. If we would have chosen: m = 2 4 1 = 15, the generated sequence would become: x1 = {3x2 +1} mod 15 = mod 15 = 1 0. 466666 15 x2 = {3x +1} mod 15 = 22 mod 15 = 2 0. 466666 15..

In this case, the generated sequence is a single number that would repeat itself indefinitely, the period of the sequence is 1, and the sequence is useless for any meaningful computations. 4.4 COMPUTER IMPLEMENTATION AND TESTING Usually the sequence repeats itself after at most m steps. It must be ensured for a given simulation that the period is longer than the needed number of random numbers. The value of m is usually chosen large enough to permit this. If a compiler does not provide a satisfactory random number generator, writing one s own generator is advisable. Figure 1 shows a random number generator subroutine, rand, which can be embedded and called from any other application. It could be reprogrammed as a function instead of a subroutine.! pseudo_random.f90! Visualizing the randomness of our own pseudo random! number generator by generating uniformly distributed! points on the unit square, for plotting with a plotting! routine, e.g. Excel.! The multiplicative congruential method:! x(i)={a*x(i-1)+c}(modulo m)! is used where:! The (modulo m) notation signifies that x(i) is the remainder! when {a*x(i-1)} is divided by m.! m is a large integer determined by the design of the computer,! usually a large power of 2.! a, c, and x(i) are integers between 0 and m-1! The numbers [x(i)/m] are used as the pseudo-random sequence.! M. Ragheb, Univ. of Illinois at Urbana-Champaign.! program pseudo_random real x, y, rr integer :: trials = 1000! Initialize output file of uniformly distributed random! numbers on the unit square open(44, file = 'random_out1') do i= 1, trials call rand(rr) x=rr call rand(rr) y=rr write (44,100) x, y end do 100 format (2f10.3) end subroutine rand(rr) real rr, xx1, xm integer x1 integer :: x0 = 2 integer :: c = 1! integer :: a = 3

integer :: a = 3*1 integer :: m = 2**20 xm = m x1 = (a*x0 + c) x1 = mod (x1, m)! write(*,*) x1 xx1 = x1 rr = xx1 / xm! write(*,*) rr x0 = x1 return end Figure 1. Procedure for generating and visual testing a pseudorandom sequence on the unit square. y 1 0.8 0.6 0.4 0.2 0 x 0 0.2 0.4 0.6 0.8 1 Fig. 2: Pseudorandom sequence plotted on the unit square, N=1,000. There are formal statistical tests that are available for testing the randomness of the generated sequence. These must be used whenever possible. A fast test would be to consider each pair of consecutive points in the sequence as points in the unit square. A scatter display of these points would visually show a random pattern. A bad sequence would visually display a banding pattern whenever the period of the sequence is short and the sequence starts repeating itself. Figure 2 shows 1,000 points that display a satisfactory choice of the parameters: x0 = 2, c=1, a = 51, m = 2 20. On the other hand, just changing the value of a in the routine into:

a = 3, leads to a short sequence, which can be detected visually as banding as shown in Fig. 3. y 1 0.8 0.6 0.4 0.2 0 0 0.2 0.4 0.6 0.8 1 x Fig. 3: Banding on the unit square for a repeating pseudorandom sequence, N=1,000. 4.5 PACKAGED PSEUDORANDOM SEQUENCES GENERATORS Most compilers and program libraries contain well-tested pseudorandom sequences generators. The International Mathematical and Statistical program library, IMSL, contains a variety of these programs. It is still advisable to test these generators whenever they are used, to make sure that they are being implemented correctly. The procedure in Fig. 4 calls the compiler s pseudorandom number generator, random, and places each two consecutive number as a vector of points in the plane. The file on which they are written can then be loaded into a plotting routine and displayed in the scatter mode.! plot_random_numb.f90! Visualising the randomness of the compiler's random! number generator by generating uniformly distributed! points on the unit square, for visualization in a! plotting routine, e.g. Excel.! M. Ragheb, Univ. of Illinois at Urbana-Champaign! program plot_random_numb real x, y integer :: trials = 1000! Initialize output file of uniformly distributed random! numbers on the unit square open(44, file = 'random_out1')

do i= 1, trials call random(rr) x=rr call random(rr) y=rr write (44,100) x, y end do 100 format (2f10.3) end Fig. 4: Procedure to visualize a compiler s random number generator. 4.6 QUASI RANDOM SEQUENCES Rather than using a pseudorandom sequence, a good sampling of the unit interval may be possible by using quasi random sequences as suggested by Zaremba and Halton. In this case an example of a sequence of the unit interval being halved, and then each subdivision is halved again can generate the following quasi random sequence: 0.5 0.25 0.125.. 0.5 0.325.. 0.625.. 0.825.. Sampling the unit interval uniformly is not a goal by itself. Beyond allowing us to uniformly sample the unit interval, pseudorandom or quasi random sequences allow us to go the extra step of sampling any discrete or continuous probability density function and thus allow us to simulate any needed effect or process that can be represented by a probability density function. 4. DISCUSSION The incentive to design and build computer platform with a larger number of bits in its word length is not mandated just by the need to obtain higher accuracies by retaining a large number of significant digits, the need to generate long encryption cipher keys, or by the need to generate large magnitudes in the address registers so as to manipulate long vectors and large matrices. It is also mandated by the need to generate unrepeated long sequences of random numbers in Monte Carlo simulations. This affects the costs of the computing machinery. A 32 bits or 64 bits desktop computer or workstation costs in the range of the thousands of dollars, whereas a supercomputer with 128 bits of word length costs in the range of the million of dollars. Desktops and workstations are satisfactory for most practical simulations. If more ambitious computations are contemplated with a need of a large number of simulations, one should consider the migration of the work from a desktop machine or a workstation to a computing platform with a larger word length. A word of caution must be stated here about processors that could process data with a reported word length of say 64 bits, but

then send the data on 32 bits data buses. In this case half the bits are lost, and the computer becomes effectively a 32 bits machine. The need to check the capabilities of the computing platform used, the adequacy of the random number generator adopted, and the randomness and periods of the generated random sequences in a given computation cannot be overemphasized. EXERCISES 1. Instead of visualizing the pseudorandom sequence on the unit square, modify the procedure given above and use each three consecutive numbers in the sequence to generate a point (xi, xi+1, xi+2) in the unit cube. Display both a good and a bad sequence of pseudorandom numbers. 2. Use the simpler form of the congruential multiplicative where c = 0, and investigate the conditions under which good and bad random sequences are generated.