From: Arjen Markus <arjen.markus@wl...>  20080630 06:42:35

Werner Smekal wrote: >Hi Alan, > > > >>We need a volunteer to step forward to change example 21 in C so that it >>uses a simple random number generator that is internal to the example >>(perhaps something extremely simple but reasonably effective if you pick a >>"good" seed such as the middle square method described at >>http://en.wikipedia.org/wiki/Middlesquare_method) and also so that >>execution times are not made part of PLplot labels. Once these changes were >>propagated to the rest of the front ends, this should hopefully make example >>21 results uniform for all frontends. >> >> > >Coincidently, I was thinking about a RNG provided by the PLplot library. > If nobody uses it except our example 21, no problem, since the code of >RNGs is rather short, so no harm done here, and maybe some users find it >useful though. A very good one (passed the diehard test), although maybe >not good enough for encryption (don't think that will be a problem :), >is the Mersenne Twister RNG > >http://www.math.sci.hiroshimau.ac.jp/~mmat/MT/emt.html > >There is a C implementation available and the code is completely free. >We could write a PLplot interface to this RNG, and then make the changes >to all bindings. I volunteer to do that (that is the RNG interface, not >all changes to the bindings), but only in two weeks, since I'm away for >one week now. > > I do not want to sound too critical, but do we need the Mersenne twister? It is a very nice thing, but wouldn't a linear congruential one do? We are not looking for the ultimate in PRNGs, are we? :) Here is a PRNG I picked up from Tcl (8.4): /* * Generate the random number using the linear congruential * generator defined by the following recurrence: * seed = ( IA * seed ) mod IM * where IA is 16807 and IM is (2^31)  1. The recurrence maps * a seed in the range [1, IM  1] to a new seed in that same range. * The recurrence maps IM to 0, and maps 0 back to 0, so those two * values must not be allowed as initial values of seed. * * In order to avoid potential problems with integer overflow, the * recurrence is implemented in terms of additional constants * IQ and IR such that * IM = IA*IQ + IR * None of the operations in the implementation overflows a 32bit * signed integer, and the C type long is guaranteed to be at least * 32 bits wide. * * For more details on how this algorithm works, refer to the following * papers: * * S.K. Park & K.W. Miller, "Random number generators: good ones * are hard to find," Comm ACM 31(10):11921201, Oct 1988 * * W.H. Press & S.A. Teukolsky, "Portable random number * generators," Computers in Physics 6(5):522524, Sep/Oct 1992. */ #define RAND_IA 16807 #define RAND_IM 2147483647 #define RAND_IQ 127773 #define RAND_IR 2836 #define RAND_MASK 123459876 tmp = iPtr>randSeed/RAND_IQ; iPtr>randSeed = RAND_IA*(iPtr>randSeed  tmp*RAND_IQ)  RAND_IR*tmp; if (iPtr>randSeed < 0) { iPtr>randSeed += RAND_IM; } (This is under BSD license) Regards, Arjen 