| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: urind.c,v 2.3 2003/02/25 02:47:22 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* Compute pseudo-asyncronous entry point for urand(3)
|
| 6 |
*/
|
| 7 |
|
| 8 |
#include "copyright.h"
|
| 9 |
#include "random.h"
|
| 10 |
|
| 11 |
#define NBITS 32 /* number of bits in an integer */
|
| 12 |
|
| 13 |
static char bctab[256] = {
|
| 14 |
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
|
| 15 |
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
| 16 |
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
| 17 |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
| 18 |
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
| 19 |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
| 20 |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
| 21 |
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
| 22 |
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
| 23 |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
| 24 |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
| 25 |
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
| 26 |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
| 27 |
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
| 28 |
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
| 29 |
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
|
| 30 |
};
|
| 31 |
|
| 32 |
#if NBITS==32
|
| 33 |
#define bitcount(i) (bctab[(i)>>24&0xff]+bctab[(i)>>16&0xff]+ \
|
| 34 |
bctab[(i)>>8&0xff]+bctab[(i)&0xff])
|
| 35 |
#endif
|
| 36 |
#if NBITS==16
|
| 37 |
#define bitcount(i) (bctab[(i)>>8&0xff]+bctab[(i)&0xff])
|
| 38 |
#endif
|
| 39 |
|
| 40 |
|
| 41 |
int
|
| 42 |
urind(s, i) /* compute i'th index from seed s */
|
| 43 |
int s, i;
|
| 44 |
{
|
| 45 |
register int ss, k;
|
| 46 |
int left;
|
| 47 |
|
| 48 |
ss = s*1103515245 + 12345;
|
| 49 |
left = 0;
|
| 50 |
for (k = i/NBITS; k--; ) {
|
| 51 |
left += bitcount(ss);
|
| 52 |
ss = ss*1103515245 + 12345;
|
| 53 |
}
|
| 54 |
for (k = i&(NBITS-1); k--; ss >>= 1)
|
| 55 |
left += ss & 1;
|
| 56 |
if (ss & 1)
|
| 57 |
return(s-left-1);
|
| 58 |
return(s-left+i);
|
| 59 |
}
|