I'm expecting lKolizji variable to be around 128, but it's much higher for large ammount of generated numbers and "boxes". The results for smaller numbers are good. I have no idea why is this happening. Here is my code with example parameters that give wrong answer. Example of good result(around 128) is int lPrzedzialow=1000000; int iLiczb = 16000;
#include <iostream>
#include <gsl/gsl_rng.h>
#include <stdlib.h>
#include<cmath>
#include <algorithm>
using namespace std;
int main (void)
{
//Random number
unsigned int seed=2596524;
gsl_rng * r=gsl_rng_alloc (gsl_rng_mt19937);
gsl_rng_set(r,seed);
gsl_rng_env_setup();
//Parameters
int lPrzedzialow=10000000000;//number of boxes
int iLiczb = 1600000;//number of random numbers
int z,lKolizji=0;//lKolizji holds collision number
vector<int> lwKomorkach(iLiczb);//number of boxes of random numbers
long double dlPrzedzialu=1./(lPrzedzialow);
//number of box of a random number
for (int i = 0; i < iLiczb; i++)
{
lwKomorkach[i] = floor((gsl_rng_uniform (r)/dlPrzedzialu));
}
//sorting
sort( lwKomorkach.begin(), lwKomorkach.end() );
//how many collisions
for(z=0;z<=iLiczb-1;z++)
{
if(lwKomorkach[z+1]==lwKomorkach[z]){lKolizji++;}
}
double pdf[lKolizji];
pdf[0]=exp(-128);
double spdf=exp(-128);
for(int h=1;h<lKolizji;h++){
pdf[h]=pdf[h-1]*128./(h);
spdf+=pdf[h];
}
double pwyzsze=1.-spdf;
cout<<endl<<lKolizji<<" "<<spdf<<" "<<pwyzsze<<endl;
gsl_rng_free (r);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire