/* * Generate, and munge a 32bit number. */ unsigned int rand32(void) { unsigned long r = 0; r = __rand32(); if (rand_bool()) { unsigned int i; unsigned int rounds; /* mangle it. */ rounds = rand() % 3; for (i = 0; i < rounds; i++) { if (rand_bool()) r |= __rand32(); else r ^= __rand32(); } } /* Sometimes deduct it from INT_MAX */ if (rand_bool()) r = INT_MAX - r; /* Sometimes flip sign */ if (rand_bool()) r |= (1L << 31); /* we might get lucky if something is counting ints/longs etc. */ if (rand() % 100 < 25) { int _div = 1 << rand_range(1, 4); /* 2,4,8 or 16 */ r /= _div; } /* limit the size */ switch (rand() % 5) { case 0: r &= 0xff; break; case 1: r &= 0xffff; break; case 2: r &= PAGE_MASK; break; case 3: r &= 0xffffff; break; case 4: // do nothing break; } return r; }
unsigned int rand32(void) { unsigned long r = 0; r = __rand32(); if (rand_bool()) { unsigned int i; unsigned int rounds; /* mangle it. */ rounds = rand() % 3; for (i = 0; i < rounds; i++) { switch (rand_bool()) { case 0: r |= __rand32(); break; case 1: r ^= __rand32(); break; default: break; } } } if (rand_bool()) r = INT_MAX - r; if (rand_bool()) r |= (1L << 31); /* limit the size */ switch (rand() % 4) { case 0: r &= 0xff; break; case 1: r &= 0xffff; break; case 2: r &= 0xffffff; break; default: break; } return r; }
static void __init_rand32(struct taus88_state *state, unsigned int seed) { int cranks = 6; #define LCG(x, seed) ((x) * 69069 ^ (seed)) state->s1 = __seed(LCG((2^31) + (2^17) + (2^7), seed), 1); state->s2 = __seed(LCG(state->s1, seed), 7); state->s3 = __seed(LCG(state->s2, seed), 15); while (cranks--) __rand32(state); }