make_seeds.cc

Go to the documentation of this file.
00001 /*
00002  *     This is rannyu as modified by A. Sokal 9/26/85.
00003  *        It is linear congruential with modulus m = 2**48, increment c = 1,
00004  *        and multiplier a = (2**36)*m1 + (2**24)*m2 + (2**12)*m3 + m4. 
00005  *        The multiplier is stored in common (see subroutine setrn)
00006  *      and is set to a = 31167285 (recommended by Knuth, vol. 2,
00007  *      2nd ed., p. 102).
00008  *
00009  *     Multiplier is 31167285 = (2**24) + 3513*(2**12) + 821.
00010  *        Recommended by Knuth, vol. 2, 2nd ed., p. 102.
00011  *     (Generator is linear congruential with odd increment
00012  *        and maximal period, so seed is unrestricted: it can be
00013  *        either even or odd.)
00014  */
00015 
00016 static int m[4] = {0, 1, 3513, 821};
00017 static int l[4] = {0, 0, 0, 1};
00018 
00019 float rannyu()
00020 {
00021   float twom12 = 1/4096.0;
00022   int i[4];
00023 
00024   i[0] = l[0]*m[3] + l[1]*m[2] + l[2]*m[1] + l[3]*m[0];
00025   i[1] = l[1]*m[3] + l[2]*m[2] + l[3]*m[1];
00026   i[2] = l[2]*m[3] + l[3]*m[2];
00027   i[3] = l[3]*m[3] + 1;
00028   l[3] = i[3] & 4095;
00029   i[2] = i[2] + (i[3] >> 12);
00030   l[2] = i[2] & 4095;
00031   i[1] = i[1] + (i[2] >> 12);
00032   l[1] = i[1] & 4095;
00033   l[0] = (i[0] + (i[1] >> 12)) >> 12;
00034   return twom12*((float)l[0] + twom12*((float)l[1] + twom12*((float)l[2] + twom12*((float)l[3]))));
00035 }
00036 
00037 /*! Seed has been set by default - this allows one to override it */
00038 void setrn(int iseed[4])
00039 {
00040   int i;
00041   for(i=0; i < 4; ++i)
00042     l[i] = iseed[i];
00043 }
00044 
00045 /*! Recover the seed */
00046 void savern(int iseed[4])
00047 {
00048   int i;
00049   for(i=0; i < 4; ++i)
00050     iseed[i] = l[i];
00051 }
00052 
00053 
00054 #include <cstdio>
00055 #include <iostream>
00056 
00057 int main(int argc, char* argv[])
00058 {
00059   if (argc != 3)
00060   {
00061     std::cerr << "Usage:  " << argv[0] << ": <num throw away> <num seeds>\n";
00062     exit(1);
00063   }
00064 
00065   int ndisc = atoi(argv[1]);
00066   int num = atoi(argv[2]);
00067 
00068   for(int n=0; n < ndisc; ++n)
00069     rannyu();
00070 
00071   for(int n=0; n < num; ++n)
00072   {
00073     float f = rannyu();
00074 
00075     int iseed[4];
00076     savern(iseed);
00077     printf("%d %d %d %d\n", iseed[1], iseed[2], iseed[3], iseed[0] & 2047);
00078   }
00079 }

Generated on Sat Jul 4 04:33:17 2009 for CHROMA by  doxygen 1.4.7