Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.mrao.cam.ac.uk/~rachael/compphys/Lecture8.pdf
Дата изменения: Tue Oct 30 16:24:04 2007
Дата индексирования: Tue Oct 2 05:00:52 2012
Кодировка:

Поисковые слова: п п п п п п п п п п п п п п п п п п п п п п
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
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, use p(y) dy = p(x) dx Hence

dx p ( y ) = p( x) dy

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!
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 · ran3 See warnings!

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).

30/10/2007

Computational Physics 2007


Variance of ran1
Simulate normal distribution by adding 12 samples and subtracting mean...

30/10/2007

Computational Physics 2007


Various generators

30/10/2007

Computational Physics 2007


About computational physics
1973: Hamming The purpose of computing is insight, not numbers Is this still true? 1973: clock speed 50kHz? memory 16kB 2007: clock speed 4GHz, memory 10GB Factor of ~220 (!) Can now solve real problems with confidence. Is this science? Or engineering?
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
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)

30/10/2007

Computational Physics 2007


General advice
It is easy to write a good computer programme BUT it is even easier to write a bad computer programme The 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).

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)
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

30/10/2007

Computational Physics 2007