正在加载图片...
7.1 Uniform Deviates 277 This corresponds to equation (7.1.1)with a 1103515245.c 12345,and m =232(since arithmetic done on unsigned long quantities is guaranteed to return the correct low-order bits).These are not particularly good choices for a and c(the period is only 230),though they are not gross embarrassments by themselves. The real botches occur when implementors,taking the committee's statement above as license,try to "improve"on the published example.For example,one popular 32-bit PC-compatible compiler provides a long generator that uses the above congruence,but swaps the high-order and low-order 16 bits of the returned value. Somebody probably thought that this extra flourish added randomness;in fact it ruins the generator.While these kinds of blunders can,of course,be fixed,there remains a fundamental flaw in simple linear congruential generators,which we now discuss. 8g The linear congruential method has the advantage of being very fast,requiring nted for only a few operations per call,hence its almost universal use.It has the disadvantage g that it is not free of sequential correlation on successive calls.Ifk random numbers at a time are used to plot points in k dimensional space (with each coordinate between 0 and 1),then the points will not tend to "fill up"the k-dimensional space,but rather will lie on (k-1)-dimensional "planes."There will be at most about m/k such planes.If the constants m,a,and c are not very carefully chosen,there will be many fewer than that.If m is as bad as 32768,then the number of planes on which triples of points lie in three-dimensional space will be no greater than about the cube root of 32768.or 32.Even if m is close to the machine's largest representable integer, e.g.,~232,the number of planes on which triples of points lie in three-dimensional space is usually no greater than about the cube root of 232,about 1600.You might Programs well be focusing attention on a physical process that occurs in a small fraction of the total volume,so that the discreteness of the planes can be very pronounced. Even worse,you might be using a generator whose choices of m,a,and c have 是 been botched.One infamous such routine,RANDU,with a =65539 and m =231 兰 to dir was widespread on IBM mainframe computers for many years,and widely copied onto other systems [1].One of us recalls producing a "random"plot with only 11 planes,and being told by his computer center's programming consultant that he had misused the random number generator:"We guarantee that each number is 御你A合过 random individually,but we don't guarantee that more than one of them is random." Figure that out Correlation in k-space is not the only weakness oflinear congruential generators. Numerical Recipes -43108. Such generators often have their low-order(least significant)bits much less random than their high-order bits.If you want to generate a random integer between I and (outside 10,you should always do it using high-order bits,as in Software. j=1+(int)(10.0*rand()/(RAND_MAX+1.0)) Ame ying of and never by anything resembling j=1+(rand()%10); (which uses lower-order bits).Similarly you should never try to take apart a "rand()"number into several supposedly random pieces.Instead use separate calls for every piece.7.1 Uniform Deviates 277 Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machine￾readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). This corresponds to equation (7.1.1) with a = 1103515245, c = 12345, and m = 232 (since arithmetic done on unsigned long quantities is guaranteed to return the correct low-order bits). These are not particularly good choices for a and c (the period is only 230), though they are not gross embarrassments by themselves. The real botches occur when implementors, taking the committee’s statement above as license, try to “improve” on the published example. For example, one popular 32-bit PC-compatible compiler provides a long generator that uses the above congruence, but swaps the high-order and low-order 16 bits of the returned value. Somebody probably thought that this extra flourish added randomness; in fact it ruins the generator. While these kinds of blunders can, of course, be fixed, there remains a fundamental flaw in simple linear congruential generators, which we now discuss. The linear congruential method has the advantage of being very fast, requiring only a few operations per call, hence its almost universal use. It has the disadvantage that it is not free of sequential correlation on successive calls. If k random numbers at a time are used to plot points in k dimensional space (with each coordinate between 0 and 1), then the points will not tend to “fill up” the k-dimensional space, but rather will lie on (k − 1)-dimensional “planes.” There will be at most about m1/k such planes. If the constants m, a, and c are not very carefully chosen, there will be many fewer than that. If m is as bad as 32768, then the number of planes on which triples of points lie in three-dimensional space will be no greater than about the cube root of 32768, or 32. Even if m is close to the machine’s largest representable integer, e.g., ∼ 232, the number of planes on which triples of points lie in three-dimensional space is usually no greater than about the cube root of 2 32, about 1600. You might well be focusing attention on a physical process that occurs in a small fraction of the total volume, so that the discreteness of the planes can be very pronounced. Even worse, you might be using a generator whose choices of m, a, and c have been botched. One infamous such routine, RANDU, with a = 65539 and m = 2 31, was widespread on IBM mainframe computers for many years, and widely copied onto other systems [1]. One of us recalls producing a “random” plot with only 11 planes, and being told by his computer center’s programming consultant that he had misused the random number generator: “We guarantee that each number is random individually, but we don’t guarantee that more than one of them is random.” Figure that out. Correlation in k-space is not the only weakness of linear congruential generators. Such generators often have their low-order (least significant) bits much less random than their high-order bits. If you want to generate a random integer between 1 and 10, you should always do it using high-order bits, as in j=1+(int) (10.0*rand()/(RAND_MAX+1.0)); and never by anything resembling j=1+(rand() % 10); (which uses lower-order bits). Similarly you should never try to take apart a “rand()” number into several supposedly random pieces. Instead use separate calls for every piece
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有