12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

112 chapter 5The linear congruent method (5.1) produces integers in the range [0,M− 1] andtherefore becomes completely correlated if a particular integer comes up a secondtime (the whole cycle then repeats). In order to obtain a longer sequence,a and M should be large numbers but not so large that ar i−1 overflows. On acomputer using 48-bit integer arithmetic, the built-in random-number generatormay use M values as large as 2 48 ≃ 3 × 10 14 . A 32-bit generator may use M =2 31 ≃ 2 × 10 9 . If your program uses approximately this many random numbers,you may need to reseed the sequence during intermediate steps to avoid the cyclerepeating.Your computer probably has random-number generators that are better thanthe one you will compute with the power residue method. You may check this outin the manual or the help pages (try the man command in Unix) and then test thegenerated sequence. These routines may have names like rand, rn, random, srand,erand, drand,ordrand48.We recommend a version of drand48 as a random-number generator. It generatesrandom numbers in the range [0, 1] with good spectral properties by using 48-bitinteger arithmetic with the parameters 2M =2 48 , c= B (base 16)=13(base 8), (5.8)a = 5DEECE66D (base 16) = 273673163155 (base 8). (5.9)To initialize the random sequence, you need to plant a seed in it. In Fortran you callthe subroutine srand48 to plant your seed, while in Java you issue the statementRandom randnum = new Random(seed); (see RandNum.java in Listing 5.1 fordetails).✞☎/ / RandNum . java : random numbers via java . util .Random . classimport java . io .∗ ; // Location of PrintWriterimport java . util .∗ ; // Location of Randompublic class RandNum {public static void main ( String [] argv ) throws IOException , FileNotFoundException {PrintWriter q = new PrintWriter (new FileOutputStream ("RandNum.DAT" ), true );long seed = 899432; // Initialize 48 bit generatorRandom randnum = new Random( seed ) ;int imax = 100; int i = 0;// generate random numbers and store in data file :for ( i =1; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!