12
30/10/2007 Computational Physics 2007 Random numbers – a note How can a computer generate a “random” number? It can’t! So generate “pseudo-random” numbers, which you hope will behave as random numbers for all practical purposes. Typical method: linear congruential generator: I J+1 = I J + c (mod m) I o is the seed. Maximal sequence length is m

Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

Random numbers – a note

• How can a computer generate a “random”number?

• It can’t!• So generate “pseudo-random” numbers, which

you hope will behave as random numbers for all practical purposes.

• Typical method:linear congruential generator:

IJ+1 = IJ + c (mod m)• Io is the seed. • Maximal sequence length is m

Page 2: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

Distributions

• Basic generator is uniform on [0,m]. Scale to make uniform on [0,1]

• To generate other distributions, usep(y) dy = p(x) dx

Hence

Need dy/dx=f(y) ; hence y(x) = F-1(x), where F is the indefinite integral of f(y). May or may not be easy to compute!

( ) ( ) dxp y p xdy

=

Page 3: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

Generators

• Many random number generators:– f95 intrinsic functions

• call random_number (harvest) . Uniform on [0,1], harvest may be array

• call random_seed (size, put, get)

– NAG functions, Chapter G05– Numerical recipes (Press et al)

• ran1• ran2• ran3See warnings!

Page 4: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

How “random”?

“We guarantee that each number is random individually, but we don’t guarantee that more than one of them is random” (see Press et al p193)

In practice, need to make a “good” choice of c and m. (see next slide).

Page 5: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

Variance of ran1 Simulate normal distribution by adding 12 samples and

subtracting mean…

Page 6: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

Various generators

Page 7: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

About computational physics1973: Hamming

The purpose of computing is insight, not numbers

Is this still true?

1973: clock speed 50kHz? memory 16kB2007: clock speed 4GHz, memory 10GB

Factor of ~220 (!)

Can now solve real problems with confidence. Is this science? Or engineering?

Page 8: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

About software

How can it go wrong?

1. Fundamentally– Wrong physics.– Numerical methods– Instability

2. Methodologically– Wrong program (bugs)– Wrong spec (What is the answer to the ultimate

question?)– Too late or slow to be useful

Page 9: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

About software (2)…

There’s one more way to go wrong:-- making the program too good!

(see Feynman quote: “Surely you’re joking Mr Feynman, p126)

Page 10: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

General advice

It is easy to write a good computer programmeBUT it is even easier to write a bad computer programmeThe difference between doing it well and doing it badly is

basically a matter of:• Thinking about the problem and designing the computer

programme first.• Care and attention to detail when writing the code.• Testing everything you do in bits which are as small as

possible (of the order of 5 lines of code).

Page 11: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

How?

Carefully!• Think first, program later• Be prepared to throw one away• Document, document, document

– Use comments, choose variable names carefully...• Think about structure • Don’t try to be clever

“What is the industry standard for the number of lines of code written by a skilled programmer each day?

5 – yes FIVE!” (MCP)

Page 12: Random numbers – a note › ~rachael › compphys › Lecture8.pdf · Random numbers – a note • How can a computer generate a “random” number? • It can’t! • So generate

30/10/2007 Computational Physics 2007

It’s all about style…

Readability – use comments, whitespace, indentation, structures, procedures…

Verify data – range checks, appropriate error handling

Write clear code that relates to the physics

Know when it’s good enough