일반적으로 사용하는 rand() 는 uniform한 난수를 발생시킨다.
normal distribution이나 exponential distribution ... 등이 필요하면 아래 사이트에서 참고하여 작성하면 되겠다.
http://www.pamvotis.org/vassis/RandGen.htm
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//Note: float variables are used. If you want to generate integer variables, just change the
//return type of the function and all other types which are related to the result...
float Constant(float mean);
float Uniform(float mean);
float Exponential(float mean);
int Poisson(float mean);
float Pareto(float alpha);
float
int Geometric(float p);
float Weibull(float scale, float shape);
float Erlang(int scale, float shape);
void main(void)
{
float mean;
//Seed is the parameter of srand (the random number generator function of C).
//srand takes an integer as a parameter. If this integer is the same,
//then each time the program is exucuted, the same random numbers
//will be generated. In order to avoid this, we put the current time as the parameter of
//the srand function.
int seed = time(NULL);
//Initiate the random number generator. The initiation must be done outside the generation
//functions, and usually is done only once.
srand(seed);
printf("Give Mean Value:");
scanf("%f",&mean);
printf("\nConstant: %f\n",Constant(mean));
printf("Uniform: %f\n",Uniform(mean));
printf("Exponential: %f\n",Exponential(mean));
printf("Poisson: %d\n",Poisson(mean));
printf("\nMore results can be added for other functions too...\n\n");
}
float Constant (float mean)
{
return mean;
}
float Uniform(float mean)
{
//Generate a random number between 0 and 1.
//REMEMBER: Always cast the oparands of a division to float, or truncation will be performed.
float R;
R = (float)rand()/(float)(RAND_MAX+1);
return 2*mean*R;
}
float Exponential(float mean)
{
//Generate a random number between 0 and 1.
//REMEMBER: Always cast the oparands of a division to float, or truncation will be performed.
float R;
R = (float)rand()/(float)(RAND_MAX+1);
//Put it as a parameter to the inverse distribution function.
return -mean*log(R);
}
int Poisson(float mean) //Special technique required: Box-Muller method...
{
float R;
float sum = 0;
int i;
i=-1;
float z;
while(sum <=mean)
{
R = (float)rand()/(float)(RAND_MAX+1);
z = -log(R);
sum+= z;
i++;
}
return i;
}
float Pareto(float alpha)
{
float R;
R = (float)rand()/(float)(RAND_MAX+1);
return (float)1/(float)(pow(R,(float)1/alpha));
}
float
{
float R1;
R1 = (float)rand()/(float)(RAND_MAX+1);
float R2;
R2 = (float)rand()/(float)(RAND_MAX+1);
return mean + stdev*cos(2*3.14*R1)*sqrt(-log(R2));
}
int Geometric(float p)
{
float R;
R= (float)rand()/(float)(RAND_MAX+1);
return (int)(log(R)/log(1-p) - 1);
}
float Weibull(float scale, float shape)
{
float R;
R= (float)rand()/(float)(RAND_MAX+1);
return scale*pow(-log(R),(float)1/shape);
}
float Erlang(int scale, float shape) //Special technique required...
{
float R=1;
int i;
for (i=1;i<=scale;i++)
R = R*((float)rand()/(float)(RAND_MAX+1));
return -shape*log(R);
}
'프로그래밍' 카테고리의 다른 글
[프로그래밍] thread 종료되지 않는 문제 (0) | 2014.02.16 |
---|---|
[프로그래밍] rand 함수의 gcc에서와 vc 에서의 차이점 (0) | 2010.03.31 |
[프로그래밍] struct pack command (구조체 최적화 하기) (0) | 2009.07.24 |