#include long semente = 12345L; #define IA 16807 #define IM 2147483647 #define AM ((float)1.0/IM) #define IQ 127773 #define IR 2836 #define MASK 123459876 /* ------------------------------------------------------- */ /* ---- protótipos/headers das funções da biblioteca ----- */ /* ------------------------------------------------------- */ // Minimal random number generator of Park and Miller. // Returns a uniform random deviate between 0.0 and 1.0. // Set or reset idum to any integer value (except the // unlikely value MASK) to initialize the sequence; // idum must not be altered between calls for // successive deviates in a sequence float ran0(long *); int gerarNumeroInteiro(int, int); float gerarNumeroReal (float, float); /* ------------------------------------------------------- */ /* -------------- implementação das funções -------------- */ /* ------------------------------------------------------- */ float ran0(long *idum) { long k; float ans; *idum ^= MASK; // XORing with MASK allows use of zero and other simple bit patterns for idum k = (*idum)/IQ; *idum = IA*(*idum-k*IQ)-IR*k; // Compute idum=(IA*idum) % IM without overflows if (*idum < 0) *idum += IM; // by Schragefs method. ans = AM*(*idum); // Convert idum to a floating result. *idum ^= MASK; // Unmask before return. return ans; } // gera aleatoriamente um número inteiro em {a, ..., b} int gerarNumeroInteiro(int a, int b) { float x; time_t seg; int inf, sup, k, cardinal; inf = a; sup = a; if (a > b) inf = b; else sup = b; seg = time(NULL); semente = semente + seg; x = ran0(&semente); semente = semente + seg; x = ran0(&semente); cardinal = (sup - inf + 1); k = (int) (x * cardinal * 10000); return inf + k % cardinal; } // gera aleatoriamente um número real em [a, b] float gerarNumeroReal(float a, float b) { float x; time_t seg; float inf, sup; inf = a; sup = a; if (a > b) inf = b; else sup = b; seg = time(NULL); semente = semente + seg; x = ran0(&semente); semente = semente + seg; x = ran0(&semente); return inf + (sup - inf) * x; }