Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<math.h>
- #include<cmath>
- #include <fstream>
- #include <gsl/gsl_rng.h>
- #include "timer.h"
- #include "time.h"
- #include <gsl/gsl_randist.h>
- #include "lab2.h"
- using namespace std;
- #define lp 5e4
- char* intToStr(int n)
- {
- int i = 0;
- char *tmp = (char*)malloc(12); // stdlib
- char *ret = (char*)malloc(12);
- if(n < 0) {
- *ret = '-';
- i++;
- n = -n;
- }
- do {
- *tmp = n % 10 + 48;
- n -= n % 10;
- if(n > 9) *tmp++;
- }
- while(n /= 10);
- while(ret[i++] = *tmp--);
- return ret;
- }
- void lab2_zad1(){
- int k[] = {11,51,101};
- for (int k_idx = 0; k_idx < 3; k_idx++)
- {
- int *histogram1, *histogram2, *histogram3;
- histogram1 = new int[k[k_idx]];
- histogram2 = new int[k[k_idx]];
- histogram3 = new int[k[k_idx]];
- for (int idx = 0; idx < k[k_idx]; idx++)
- {
- histogram1[idx]=0;
- histogram2[idx]=0;
- histogram3[idx]=0;
- }
- time_t ttt ;
- struct tm date;
- time(&ttt);
- localtime_s(&date,&ttt);
- int r=(date).tm_year+1900;
- int m=(date).tm_mon;
- int d=(date).tm_mday;
- int g=(date).tm_hour;
- int min=(date).tm_min;
- int s=(date).tm_sec;
- int X0=r+100*(m-1+12*(d-1+31*(g+24*(min+60*s))));
- gsl_rng * generatorek1, *generatorek2;
- generatorek1 = gsl_rng_alloc(gsl_rng_mt19937);
- generatorek2 = gsl_rng_alloc(gsl_rng_randu);
- gsl_rng_set(generatorek1, X0);
- gsl_rng_set(generatorek2, X0);
- srand(X0);
- for (int i = 0; i < lp; i++){
- histogram1[(int)floor(k[k_idx]*gsl_rng_uniform(generatorek1))]++;
- histogram2[(int)floor(k[k_idx]*gsl_rng_uniform(generatorek1))]++;
- histogram3[(int)floor(k[k_idx]*rand()/(double)RAND_MAX)]++;
- }
- gsl_rng_free(generatorek1);
- gsl_rng_free(generatorek2);
- if (k_idx==0)
- {
- ofstream plik1("zad1_mt_k10.txt");
- ofstream plik2("zad1_randu_k10.txt");
- ofstream plik3("zad1_rand_k10.txt");
- for (int i = 0; i < k[k_idx]; i++)
- {
- plik1 << i+1 << " " << histogram1[i] << endl;
- plik2 << i+1 << " " << histogram2[i] << endl;
- plik3 << i+1 << " " << histogram3[i] << endl;
- }
- }else if (k_idx==1)
- {
- ofstream plik1("zad1_mt_k50.txt");
- ofstream plik2("zad1_randu_k50.txt");
- ofstream plik3("zad1_rand_k50.txt");
- for (int i = 0; i < k[k_idx]; i++)
- {
- plik1 << i+1 << " " << histogram1[i] << endl;
- plik2 << i+1 << " " << histogram2[i] << endl;
- plik3 << i+1 << " " << histogram3[i] << endl;
- }
- }else{
- ofstream plik1("zad1_mt_k100.txt");
- ofstream plik2("zad1_randu_k100.txt");
- ofstream plik3("zad1_rand_k100.txt");
- for (int i = 0; i < k[k_idx]; i++)
- {
- plik1 << i+1 << " " << histogram1[i] << endl;
- plik2 << i+1 << " " << histogram2[i] << endl;
- plik3 << i+1 << " " << histogram3[i] << endl;
- }
- }
- }
- }
- void lab2_zad2(){
- time_t ttt ;
- struct tm date;
- time(&ttt);
- localtime_s(&date,&ttt);
- int r=(date).tm_year+1900;
- int m=(date).tm_mon;
- int d=(date).tm_mday;
- int g=(date).tm_hour;
- int min=(date).tm_min;
- int s=(date).tm_sec;
- int X0=r+100*(m-1+12*(d-1+31*(g+24*(min+60*s))));
- gsl_rng * generatorek1, *generatorek2;
- generatorek1 = gsl_rng_alloc(gsl_rng_mt19937);
- //generatorek2 = gsl_rng_alloc(gsl_rng_randu);
- gsl_rng_set(generatorek1, X0);
- //gsl_rng_set(generatorek2, X0);
- //srand(X0);
- int *chi_histogram;
- int num =100;
- chi_histogram= new int[num];
- for (int i = 0; i < num; i++)
- {
- chi_histogram[i]=0;
- }
- for (int chi_idx = 0; chi_idx < lp; chi_idx++)
- {
- if (chi_idx%100==0)
- {
- cout<<chi_idx<<endl;
- }
- int k[] = {10,50,100};
- for (int k_idx = 0; k_idx < 1; k_idx++)
- {
- int *histogram1/*, *histogram2, *histogram3*/;
- histogram1 = new int[k[k_idx]];
- /*histogram2 = new int[k[k_idx]];
- histogram3 = new int[k[k_idx]];*/
- for (int idx = 0; idx < k[k_idx]; idx++)
- {
- histogram1[idx]=0;
- /*histogram2[idx]=0;
- histogram3[idx]=0;*/
- }
- for (int i = 0; i < lp; i++){
- histogram1[(int)floor(k[k_idx]*gsl_rng_uniform(generatorek1))]++;
- //histogram2[(int)floor(k[k_idx]*gsl_rng_uniform(generatorek1))]++;
- //histogram3[(int)floor(k[k_idx]*rand()/(double)RAND_MAX)]++;
- }
- //gsl_rng_free(generatorek2);
- //if (k_idx==0)
- //{
- // ofstream plik1("zad2_mt_k10.txt");
- // /*ofstream plik2("zad2_randu_k10.txt");
- // ofstream plik3("zad2_rand_k10.txt");*/
- // for (int i = 0; i < k[k_idx]; i++)
- // {
- // plik1 << i+1 << " " << histogram1[i] << endl;
- // /*plik2 << i+1 << " " << histogram2[i] << endl;
- // plik3 << i+1 << " " << histogram3[i] << endl;*/
- // }
- //}else if (k_idx==1)
- //{
- // ofstream plik1("zad2_mt_k50.txt");
- // /*ofstream plik2("zad2_randu_k50.txt");
- // ofstream plik3("zad2_rand_k50.txt");*/
- // for (int i = 0; i < k[k_idx]; i++)
- // {
- // plik1 << i+1 << " " << histogram1[i] << endl;
- // /*plik2 << i+1 << " " << histogram2[i] << endl;
- // plik3 << i+1 << " " << histogram3[i] << endl;*/
- // }
- //}else{
- // ofstream plik1("zad2_mt_k100.txt");
- // /*ofstream plik2("zad2_randu_k100.txt");
- // ofstream plik3("zad2_rand_k100.txt");*/
- // for (int i = 0; i < k[k_idx]; i++)
- // {
- // plik1 << i+1 << " " << histogram1[i] << endl;
- // /*plik2 << i+1 << " " << histogram2[i] << endl;
- // plik3 << i+1 << " " << histogram3[i] << endl;*/
- // }
- //}
- double chi=0,suma=0;
- for (int i=0;i<k[k_idx];i++) suma+=pow(histogram1[i],2.0);
- chi=(float)k[k_idx]/(float)lp*suma-lp;
- //cout<<"Statystyka chi kwadrat dla MT = "<<chi<<endl;
- if (chi > 49)
- {
- chi=49;
- cout<<"chi >49" <<endl;
- }
- chi_histogram[(int)floor(chi*2)]++;
- /*chi=0,suma=0;
- for (int i=0;i<k[k_idx];i++) suma+=pow(histogram2[i],2.0);
- chi=(float)k[k_idx]/(float)lp*suma-lp;
- cout<<"Statystyka chi kwadrat dla randu = "<<chi<<endl;*/
- free(histogram1);
- //Sleep(100);
- }
- }
- ofstream plik("zad2_chi_histogram.txt");
- double suma_chi=0;
- for (int i = 0; i < 100; i++)
- {
- plik << 0.5*(i+1) << "\t" << chi_histogram[i]/lp/0.5 << endl;
- suma_chi+=chi_histogram[i]/lp/0.5;
- /*plik2 << i+1 << " " << histogram2[i] << endl;
- plik3 << i+1 << " " << histogram3[i] << endl;*/
- }
- cout<<suma_chi<<endl;
- gsl_rng_free(generatorek1);
- ofstream plik10("zad2_chi_histogram_teo.txt");
- for (int i = 0; i < 49; i++)
- {
- plik10 <<(i)*0.5 <<"\t"<<gsl_ran_chisq_pdf(i/2,10)<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement