Gaussian Pseudorandom Generator

Adakalanya dalam penelitian, kita harus menggenerate synthetic random data untuk mengamati karakteristik algoritma yg sedang dibuat. Program ini dipakai untuk meng-generate angka random dengan distribusi Gaussian pada stdev & mean tertentu.

#include
#include
#include
#define pi_25 3.141592653589793238462643
#define SEED 10

void *mymalloc( int size)
{
void*buf;
if((buf=(void*)malloc(size))==NULL)
{
fprintf(stderr,”Not enough Memory.\n”);
exit(1);
}
return(buf);
}

// Gaussian random modified from http://www-ee.uta.edu/eeweb/ip/courses/ee5352/Prog/random.c

void gaussian_random(int Random_Seed, int Num, float *x, float mean, float std)
{
int i;
time_t t1;
float tmp1,tmp2;

srand48((long)Random_Seed);
for(i=0;i<Num;i++) {
tmp1 = cos(2*pi_25*drand48());
tmp2 = sqrt( -2.0 * log(drand48()));
x[i]= mean + std * tmp1 * tmp2;
}
}

void datastat(float *x,int Num,float *Min,float *Max)
{

int i,j;
float s, var,ave,sk;

ave=0; var=0; *Max=-pow(2,10); *Min=pow(2,10);
for(i=0;i<Num;i++) {
ave+=x[i]/Num;
*Min=(*Minx[i])?*Max:x[i];
}

for(i=0;i<Num;i++) var+=(x[i]-ave)*(x[i]-ave);
// var=var/(Num-1); // sample variance
var=var/Num; // population variance

s=(float)sqrt((long)var);
for(i=0,sk=0;i<Num;i++) sk+= pow((x[i]-ave)/s,3) * (float)Num/((Num-1)*(Num-2));

fprintf(stdout,"Statistic of the the data :\n");
fprintf(stdout,"\tNum %d\n",Num);
fprintf(stdout,"\tMin %f\n\tMax %f\n",*Min,*Max);
fprintf(stdout,"\tMean %f\n",ave);
fprintf(stdout,"\tVariance %f\n",var);
fprintf(stdout,"\tSt.Dev %f\n",s);
fprintf(stdout,"\tSkewness %f\n",sk);

}

void create_frequency_distribution(float *x,float Min,float Max,int Num )
{

int i,j,k;
int *f;
int level_num;

level_num=1+(int)(log(Num)/log(2));
f=(int*)mymalloc((int)sizeof(int)*level_num);
for(k=0;k<level_num;k++) f[k]=0;
for(i=0;i=level_num) {
f[level_num-1]++;
}
else
f[k]++;
}
fprintf(stdout,”Frequency Polygon:\n”);
for(k=0;k<level_num;k++) {
fprintf(stdout,"%f\t%d\n",Min+(2*k+1)*(Max-Min)/(level_num*2),f[k]);
}
free(f);

}

main(int argc,char **argv)
{
int i;
int Dim,Num;
float *dat,Mean,SD,Min,Max;

if(argc!=4) {
fprintf(stderr,"Usage : %s Mean Stdev Data_Num\n",argv[0]);
exit(1);
}
Mean=atof(argv[1]);
SD=atof(argv[2]);
Num=atoi(argv[3]);
dat=(float*)mymalloc((int)sizeof(float)*Num);
gaussian_random(SEED,Num,dat,Mean,SD);
datastat(dat,Num,&Min,&Max); // statistical information of dat[]
create_frequency_distribution(dat,Min,Max,Num);
fprintf(stdout,"Data:\n");
for(i=0;i<Num;i++) fprintf(stdout,"%d\t%f\n",i,dat[i]); // print the pseudo-random data
free(dat);
}

Iklan

Tentang Anto Satriyo Nugroho

My name is Anto Satriyo Nugroho. I am working as research scientist at Center for Information & Communication Technology, Agency for the Assessment & Application of Technology (PTIK-BPPT : Pusat Teknologi Informasi & Komunikasi, Badan Pengkajian dan Penerapan Teknologi). I obtained my doctoral degree (Dr.Eng) from Nagoya Institute of Technology, Japan in 2003. My office is located in Serpong, Tangerang Selatan City. My research is on pattern recognition and image processing with applied field of interests on biometrics identification & development of computer aided diagnosis for Malaria. Should you want to know further information on my academic works, please visit my professional site at http://asnugroho.net
Pos ini dipublikasikan di living in Japan. Tandai permalink.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s