/* This program by D E Knuth is in the public domain and freely copyable *ASLONGASYOUMAKEABSOLUTELYNOCHANGES! *ItisexplainedinSeminumericalAlgorithms,3rdedition,Section3.6 *(orintheerratatothe2ndedition---see *http://www-cs-faculty.stanford.edu/~knuth/taocp.html
* in the changes to Volume 2 on pages 171 and following). */
/* N.B. The MODIFICATIONS introduced in the 9th printing (2002) are
included here; there's no backwards compatibility with the original. */
/* If you find any bugs, please report them immediately to *taocp@cs.stanford.edu
* (and you will be rewarded if the bug is genuine). Thanks! */
/************ see the book for explanations and caveats! *******************/ /************ in particular, you need two's complement arithmetic **********/
#include"naurng.h"
#define KK 100/* the long lag */ #define LL 37/* the short lag */ #define MM (1L<<30) /* the modulus */ #define mod_diff(x,y) (((x)-(y))&(MM-1)) /* subtraction mod MM */
static TLS_ATTR long ran_x[KK]; /* the generator state */
staticvoid
ran_array(long aa[],int n)
{ int i,j; for (j=0;j<KK;j++) aa[j]=ran_x[j]; for (;j<n;j++) aa[j]=mod_diff(aa[j-KK],aa[j-LL]); for (i=0;i<LL;i++,j++) ran_x[i]=mod_diff(aa[j-KK],aa[j-LL]); for (;i<KK;i++,j++) ran_x[i]=mod_diff(aa[j-KK],ran_x[i-LL]);
}
/* the following routines are from exercise 3.6--15 */ /* after calling ran_start, get new randoms by, e.g., "x=ran_arr_next()" */
#define RNG_QUALITY 1009/* recommended quality level for high-res use */ static TLS_ATTR long ran_arr_buf[RNG_QUALITY]; staticlong ran_arr_dummy=-1; staticlong ran_arr_started=-1; static TLS_ATTR long *ran_arr_ptr = &ran_arr_dummy;
#define TT 70/* guaranteed separation between streams */ #define is_odd(x) ((x)&1) /* units bit of x */
staticvoid
ran_start(long seed)
{ int t,j; long x[KK+KK-1]; /* the preparation buffer */ long ss=(seed+2)&(MM-2);
for (j=0;j<KK;j++) {
x[j]=ss; /* bootstrap the buffer */
ss<<=1; if (ss>=MM) ss-=MM-2; /* cyclic shift 29 bits */
}
x[1]++; /* make x[1] (and only x[1]) odd */ for (ss=seed&(MM-1),t=TT-1; t; ) { for (j=KK-1;j>0;j--) x[j+j]=x[j], x[j+j-1]=0; /* "square" */ for (j=KK+KK-2;j>=KK;j--)
x[j-(KK-LL)]=mod_diff(x[j-(KK-LL)],x[j]),
x[j-KK]=mod_diff(x[j-KK],x[j]); if (is_odd(ss)) { /* "multiply by z" */ for (j=KK;j>0;j--) x[j]=x[j-1];
x[0]=x[KK]; /* shift the buffer cyclically */
x[LL]=mod_diff(x[LL],x[KK]);
} if (ss) ss>>=1; else t--;
} for (j=0;j<LL;j++) ran_x[j+KK-LL]=x[j]; for (;j<KK;j++) ran_x[j-LL]=x[j]; for (j=0;j<10;j++) ran_array(x,KK+KK-1); /* warm things up */
ran_arr_ptr=&ran_arr_started;
}
void
ran_init(long seed) /* Added by BDM: use instead of ran_start. */
{
ran_start((unsignedlong)seed % (MM-2));
}
long
ran_init_time(long extra) /* Added by BDM: use the real time and the argument to initialise. Returnsthevalueoftheseed,sothesamesequencecanbe obtainedagainbycallingran_init(seed).
*/
{ double t;
nauty_counter ul; /* 64-bit unsigned */ long seed;
REALTIMEDEFS
t = NAUTYREALTIME; if (t > 1660000000.0) ul = (nauty_counter)(t*2100001.0); else ul = (nauty_counter)(t+212300021.0);
ul ^= (nauty_counter)(extra) * 997;
seed = (long)ul;
ran_init(seed);
return seed;
}
staticlong
ran_arr_cycle(void) /* Modified by BDM to automatically initialise
if no explicit initialisation has been done */
{ if (ran_arr_ptr==&ran_arr_dummy)
ran_start(314159L); /* the user forgot to initialize */
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.