#ifndef lint static const char RCSid[] = "$Id: urand.c,v 2.12 2022/04/21 02:52:40 greg Exp $"; #endif /* * Anticorrelated random function due to Christophe Schlick */ #include "copyright.h" #include "rterror.h" #include "random.h" #undef initurand #define MAXORDER (8*sizeof(unsigned short)) static unsigned short empty_tab = 0; unsigned short *urperm = &empty_tab; /* urand() permutation */ int urmask = 0; /* bits used in permutation */ long irandom( /* better than using random() % modulus */ long modulus ) { static const int isize = sizeof(int); static const int lsize = sizeof(long); static const int llsize = sizeof(long long); if ((lsize == 8) | (isize == 8)) return((random()*modulus)>>31); if (llsize == 8) return((random()*(long long)modulus)>>31); return(random() % modulus); } int initurand( /* initialize urand() for size entries */ int size ) { int order, n; int i, offset; if ((urperm != NULL) & (urperm != &empty_tab)) free((void *)urperm); if (--size <= 0) { empty_tab = 0; urperm = &empty_tab; urmask = 0; return(0); } for (i = 1; (size >>= 1); i++) if (i == MAXORDER) break; order = i; urmask = (1< 0) hval ^= *d++ * tab[n&7]; return(hval & 0x7fffffff); }