ProjectEuler/c++/lib/random.h
2013-04-17 14:34:39 +08:00

32 lines
860 B
C

#include <stdlib.h>
#include <time.h>
#include <math.h>
int random(int a_random, int b_random);
double random(double c_random, double d_random, int accu);
int random(int a_random, int b_random)
{
if(a_random >= b_random) return 0;
int temp_int_random;
int int_random = 0;
int accu = b_random - a_random;
if(accu < 10000) accu = 10000;
temp_int_random = time(NULL) % (accu - 1) + 1;
for(int i_int_random = 1; i_int_random <= temp_int_random; i_int_random++)
int_random = (int_random + rand()) % accu;
int_random = int_random % (b_random - a_random) + a_random;
return int_random;
}
double random(double a_random, double b_random, int accu = 4)
{
if(a_random >= b_random) return 0;
double temp = 0.0;
for(int i = 1; i <= accu; i++) {
temp += double(random(0, 10)) * pow(0.1, double(i));
}
return temp * (b_random - a_random) + a_random;
}