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 nilai standard deviation dan mean yang diinginkan. Cara kompilasi dan menjalankannya adalah sebagaimana screenshot berikut:

Screen Shot 2017-12-21 at 17.53.51

Kalau distribusi frekuensi tersebut diplot, akan diperoleh grafik sebagai berikut:

Screen Shot 2017-12-21 at 17.29.12

 

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#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, last access : March 5, 2011

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

srand48( (long)Random_Seed );
for( i = 0; i < Num; i++) {
tmp1 = cos( 2 * M_PI * 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 = (*Min < x[i]) ? *Min : x[i];
*Max = (*Max > x[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, bin_no;
float interval;

level_num = 1+ (int)( 0.5 + log(Num)/ log(2) );
interval = (float)( ( Max - Min )/ ( level_num -1 ) );

f = (int*)mymalloc( (int)sizeof(int) * level_num );

for( k = 0; k < level_num; k++) f[k] = 0;
for(i = 0; i < Num ; i++ ) {
bin_no = (int)( ( x[ i ] - Min ) / interval );
f[ bin_no ] ++;
}

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

int 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);

return 0;
}

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 )

Foto Google+

You are commenting using your Google+ 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 )

w

Connecting to %s