#define P_RAND_MAX 2147483647
static unsigned long p_next = 1;
int p_rand (void) {
int result;
/* ANSI-C portable implementation */
p_next = p_next * 1103515245 + 12345;
result = p_next & P_RAND_MAX;
/* improve randomness by swapping byte 1 with byte 3 */
result = (result & 2130771712) | ((result & 255) << 16) | ((result & 16711680) >> 16);
return result;
}
void p_srand (unsigned int seed) {
p_next = seed;
}
#define P_RAND_MAX 2147483647
static unsigned long p_next = 1;
int p_rand (void) {
int result;
/* ANSI-C portable implementation */
p_next = p_next * 1103515245 + 12345;
result = p_next & P_RAND_MAX;
/* improve randomness by swapping byte 1 with byte 3 */
result = (result & 2130771712) | ((result & 255) << 16) | ((result & 16711680) >> 16);
return result;
}
void p_srand (unsigned int seed) {
p_next = seed;
}
/*
* I've gotten too many bad reports on OS-supplied random number generators.
* This is the Mitchell-Moore algorithm from Knuth Volume II.
* Best to leave the constants alone unless you've read Knuth.
* – Furey
*/
static int rgiState[2 + 55];
void init_mm( )
{
int *piState;
int iState;
piState = &rgiState[2];
piState[-2] = 55 - 55;
piState[-1] = 55 - 24;
piState[0] = ( ( int )current_time ) & ( ( 1 << 30 ) - 1 );
piState[1] = 1;
for( iState = 2; iState < 55; iState++ )
{
piState[iState] = ( piState[iState - 1] + piState[iState - 2] ) & ( ( 1 << 30 ) - 1 );
}
return;
}
int number_mm( void )
{
int *piState;
int iState1;
int iState2;
int iRand;
piState = &rgiState[2];
iState1 = piState[-2];
iState2 = piState[-1];
iRand = ( piState[iState1] + piState[iState2] ) & ( ( 1 << 30 ) - 1 );
piState[iState1] = iRand;
if( ++iState1 == 55 )
iState1 = 0;
if( ++iState2 == 55 )
iState2 = 0;
piState[-2] = iState1;
piState[-1] = iState2;
return iRand >> 6;
}
#define L 37U
#define K 100U
static unsigned long int sequence[K];
static unsigned int b = L, a = K;
void init_mm(unsigned long int seed) {
unsigned int i;
for (i = 0; i < K * 2; i++)
sequence[i % K] = seed = (1664525 * seed + 1013904223);
return;
}
unsigned long int number_mm(void) {
a++;
b++;
return sequence[a % K] += sequence[b % K];
}
If you think there is a bug, why not explain it and give us a patch?
You stated it yourself: rand was perticulary bad in mst OS's at the beginning of them. It is not the case anymore. Unless you want a perticualr behaviour for specific reasons ( I doubt you would in a mud) you are way better using the method I gave to have a real random distribution. Because Random is not only about getting a random number in the first place. It is also in not getting more of some than other.
No, not in error, just unlikely.