#include <random>
#include <chrono>
void save(int n,string fname, double *x1,double *x2){
int i;
FILE *p = fopen(fname.c_str(),"w");
for(i=0;i<n;i++)fprintf(p,"%.10f\t %.10f\t \n",x1[i],x2[i]);
fclose(p);
}//save()
int comp_doub_ascend(const void * a, const void * b) {
if (*(double*)a > *(double*)b) return 1;
else if (*(double*)a < *(double*)b) return -1;
else return 0;
}
void cum_dist(int n,double *y,double *cd){
int i;
qsort(y,n,sizeof(double),comp_doub_ascend);
for(i = 0;i < n;i++)cd[i] = 100.* double(i)/double(n-1);
}//cum_dist()
double median(int n,double *y){
//y is sorted ascending
int i ;
double c;
for(i=0;i<n;i++){
c = double(i)/double(n-1);
if(c >= 0.5)break;
}
return y[i];
}//median()
double avg(int n,double *y){
int i;
double s = 0,av;
for(i=0;i<n;i++)s += y[i];
return s/double(n);
}//avg()
double sdev(int n,double *y){
int i;
double s = 0,av;
for(i=0;i<n;i++)s += y[i];
av = s/double(n);
s = 0;
for(i=0;i<n;i++)s += pow(av-y[i],2);
return sqrt(s/double(n));
}//sdev()
double minf(int n,double *y){
int i;
double ymin = y[0];
for(i=0;i<n;i++)if(y[i] < ymin)ymin = y[i];
return ymin;
}
double maxf(int n,double *y){
int i;
double ymax = y[0];
for(i=0;i<n;i++)if(y[i] > ymax)ymax = y[i];
return ymax;
}
void normal(int n,double u,double std,double *y){
int i;
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
default_random_engine generator (seed);
normal_distribution<double> dist(u, std);
for(i=0;i<n;i++)y[i] = dist(generator);
}
void expon(int n,double mean,double *y){
int i;
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
default_random_engine generator (seed);
exponential_distribution<double> dist(1./mean);
for(i=0;i<n;i++)y[i] = dist(generator);
}
int main(){
int n2=10000;
int i, n =100,yi[n];
double *yn = new double[n2];
double *ye = new double[n2];
double *cde = new double[n2];
double *cdn = new double[n2];
double minn,maxn,mine,maxe;
double avgn,stdn,medn,avge,stde,mede;
normal(n2,125,20,yn);
expon(n2,20,ye);
cum_dist(n2,ye,cde);
avgn = avg(n2,yn);
cum_dist(n2,ye,cde);
cum_dist(n2,yn,cdn);
stdn = sdev(n2,yn);
medn = median(n2,yn);
minn = minf(n2,yn);
maxn = maxf(n2,yn);
printf("normal %.5f \t %.5f \t %.5f \t %.5f\n",avgn,stdn,medn,(maxn-minn)/stdn);
avge = avg(n2,ye);
stde = sdev(n2,ye);
mede = median(n2,ye);
printf("exponential %.5f \t %.5f \t %.5f %.5f\n",avge,stde,mede,log(2)*avge);
save(n2,"norm.dat", yn,cdn);
save(n2,"exp.dat", ye,cde);
return 0;
}