#include #include /* * This is a simple random number generator with good quality for audio purposes. * It consists of two polycounters with opposite rotation direction and different * periods. The periods are coprime, so the total period is the product of both. * * ------------------------------------------------------------------------------------------------- * +-> |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| * | ------------------------------------------------------------------------------------------------- * | | | | | | | * | +--+--+--+-XOR-+--------+ * | | * +--------------------------------------------------------------------------------------+ * * ------------------------------------------------------------------------------------------------- * |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| <-+ * ------------------------------------------------------------------------------------------------- | * | | | | | * +--+----XOR----+--+ | * | | * +----------------------------------------------------------------------------------------+ * * * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481, * which gives a period of 18.410.713.077.675.721.215. The result is the * XORed values of both generators. */ static const unsigned char Parity [256] = { // parity 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0 }; static uint32_t rnd_r1 = 1; static uint32_t rnd_r2 = 1; void set_seed ( uint32_t seed ) { rnd_r1 = seed != 0 ? seed : 1; // 0 is a forbidden value which locks the generator rnd_r2 = 1; // the are several (8389119) forbidden codes. 1 is not one of them } uint32_t random_int ( void ) { uint32_t t1, t2, t3, t4; t3 = t1 = rnd_r1; t4 = t2 = rnd_r2; // Parity calculation is done via table lookup, this is also available t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations. t1 <<= 31; t2 = Parity [t2]; return (rnd_r1 = (t3 >> 1) | t1 ) ^ (rnd_r2 = (t4 + t4) | t2 ); } double Random_Equal ( double mult ) // gives a equal distributed random number { // between -2^31*mult and +2^31*mult return mult * (int32_t) random_int (); } double Random_Normal ( void ) // gives a normal distributed random number { return sqrt (-2. * log ( (random_int () + 1) * (1./4294967296.)) ) * cos ( (2.*M_PI/4294967296.) * random_int () ); } double Random_Exponential ( double mult ) { return -log ( (1. + random_int ()) * (1./4294967296.) ); } double Random_Triangular ( double mult ) { return mult * ((double)(int32_t) random_int () + (double)(int32_t) random_int ()); } /* end of random.c */