// ============================================================ // // random.cc // // Copyright 2002, Dennis Meilicke and Rene Peralta // // ============================================================ // // Description: // // Generate random numbers. // // To do: // // Re-implement the random sign stuff // // This shouldn't be a member of ln, should it? // Shouldn't this be a Base function? // // ============================================================ #include #include // ============================================================ static const digit_t mod_const = 32609; static digit_t Xn_1 = 0; static digit_t Xn_2 = 0; static digit_t Xn_3 = 0; // ============================================================ static void Initialize( void ) { struct timeval tv; if (Xn_1 == 0) { gettimeofday(&tv,0); Xn_1 = (tv.tv_usec + 1) % mod_const; } if (Xn_2 == 0) { gettimeofday(&tv,0); Xn_2 = (tv.tv_usec + 1) % mod_const; } if (Xn_3 == 0) { gettimeofday(&tv,0); Xn_3 = (tv.tv_usec + 1) % mod_const; } } // ============================================================ static inline digit_t RandomBits( void ) { struct timeval tv; gettimeofday( &tv, 0 ); Xn_1 = ( Xn_3 * Xn_2 + tv.tv_usec + 7 ) % mod_const; Xn_2 = ( Xn_3 * Xn_1 + tv.tv_usec + 3 ) % mod_const; Xn_3 = ( Xn_1 * Xn_2 + tv.tv_usec + 11 ) % mod_const; return Xn_1; } // ============================================================ ln & ln::Random( size_t digits ) // // Description // // Generate a random LargeNumber // // To Do: This should not call the time of day system call for // every digit. Once for each function call should be // enough. Also, we should be generating random ln++ // digits, not random decimal digits. Then calcualte // the number of bits, and truncate the most // significant digit. Note: if the most significant // bit of the truncated digit is zero, select another // random msd, and truncate again. Continue until a 1 // bit is generated. // // This method is *slow*, and the above should speed it // up, considerably. // { if (digits <= 0) return ( *this = 0 ); Initialize( ); // // The size of the buffer should really come from // the traits object... // char buffer[maxDigitsBase10]; size_t i; for( i=0 ; i < digits ; i++ ) { buffer[i] = (char)(RandomBits( ) % 10) + '0'; } buffer[i] = 0; return ( *this = buffer ); } // ============================================================ #ifdef UNDER_CONSTRUCTION ln& ln::RandomMod( const ln& mod ) { size_t numDigits = mod.GetSize( ); size_t i; for( i=0 ; iSetDigit( i, temp ); } // // At this point, what do we do? Do we // just take the mod of the new number? // Or do we try to do something "smart"? // } #endif // ============================================================