home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / RADIANCE / SRC / COMMON / URAND.C < prev    next >
C/C++ Source or Header  |  1993-10-07  |  1KB  |  65 lines

  1. /* Copyright (c) 1992 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)urand.c 2.2 9/5/92 LBL";
  5. #endif
  6.  
  7. /*
  8.  * Anticorrelated random function due to Christophe Schlick
  9.  */
  10.  
  11. #include  "random.h"
  12.  
  13. #define  NULL        0
  14.  
  15. extern char  *malloc();
  16.  
  17. short  *urperm = NULL;    /* urand() permutation */
  18. int  urmask;        /* bits used in permutation */
  19.  
  20.  
  21. initurand(size)        /* initialize urand() for size entries */
  22. int  size;
  23. {
  24.     int  order, n;
  25.     register int  i, offset;
  26.  
  27.     if (urperm != NULL)
  28.         free((char *)urperm);
  29.     size--;
  30.     for (i = 1; size >>= 1; i++)
  31.         ;
  32.     order = i;
  33.     urmask = (1<<i) - 1;
  34.     urperm = (short *)malloc((urmask+1)*sizeof(short));
  35.     if (urperm == NULL) {
  36.         eputs("out of memory in initurand\n");
  37.         quit(1);
  38.     }
  39.     urperm[0] = 0;
  40.     for (n = 1, offset = 1; n <= order; n++, offset <<= 1)
  41.         for (i = offset; i--; ) {
  42.             urperm[i] =
  43.             urperm[i+offset] = 2*urperm[i];
  44.             if (random() & 0x4000)
  45.                 urperm[i]++;
  46.             else
  47.                 urperm[i+offset]++;
  48.         }
  49. }
  50.  
  51.  
  52. int
  53. ilhash(d, n)            /* hash a set of integer values */
  54. register int  *d;
  55. register int  n;
  56. {
  57.     static int  tab[8] = {13623,353,1637,5831,2314,3887,5832,8737};
  58.     register int  hval;
  59.  
  60.     hval = 0;
  61.     while (n-- > 0)
  62.         hval += *d++ * tab[n&7];
  63.     return(hval);
  64. }
  65.