int r[100]; // "global" pseudo-random table --
// must be visible to rand and init_rand
//
// return a random number in the range 0 to 999999999
//
int rand (void)
{
int i = r[98];
int j = r[99];
int k;
int t;
if ((t = r[i] - r[j]) < 0) t += 1000000000L;
r[i] = t;
r[98]--; r[99]--;
if (r[98] == 0) r[98] = 55;
if (r[99] == 0] r[99] = 55;
k = r[100] % 42 + 56;
r[100] = r[k];
r[k] = t;
return(r[100]);
}
//
// seed the random number table
//
int init_rand (char *seed)
{
char buf[101];
int i, j, k;
if (strlen(seed) > 85) return(0);
sprintf(buf, "aEbFcGdHeI%s", seed);
while (strlen(buf) < 98) strcat(buf, "Q");
for (i = 1; i < 98; i++)
r[i] = buf[i] * 8171717 + i * 997;
i = 97; j = 12;
for (k = 1; k < 998; k++) {
r[i] -= r[j];
if (r[i] < 0) r[i] += 1000000000;
i--; j--;
if (i == 0) i=97;
if (j == 0) j=97;
}
r[98] = 55;
r[99] = 24;
r[100] = 77;
}
//
// return a random int between a and b
// assumes init_rand already called.
//
int rand_int(int a, int b)
{
return (a + rand() % (b - a + 1));
}
|