均匀分布:线性同余法
正态分布:sum-of-12 method
其他分布:逆变换法
/***************************************** Copyright (c) 2015 Jingshuang Hu @filename:demo.c @datetime:2015.10.18 @author:HJS @e-mail:[email protected] @blog:http://blog.csdn.net/hujingshuang *****************************************/ #include <stdio.h> #include <stdlib.h> #include <math.h> /**********************************************************************************************/ void menu(void); double *Uniform_Distribution(unsigned int length, unsigned int num); void Demo_Gaussian(void); double *Gaussian_Distribution(double miu, double sigma, int length); void Demo_Rayleigh(void); double *Rayleigh_Distribution(double sigma, int length); void Demo_Poisson(void); int *Poisson_Distribution(double lamda,unsigned int length); /**********************************************************************************************/ int main() { menu(); return 0; } /**********************************************************************************************/ void menu(void) { int num = 0, flag = 0; while(!flag) { printf("---------random variates---------\n"); printf("1.gaussian distribution\n2.rayleigh distribution\n3.poisson distribution\n4.exit\n"); printf("---------------------------------\nyour choice<1,2,3,4>:"); scanf("%d", &num); switch(num) { case 1: Demo_Gaussian();break; case 2: Demo_Rayleigh();break; case 3: Demo_Poisson(); break; case 4: flag = 1; break; default: break; } printf("Save success,press any key to continue.\n"); getchar();getchar(); fflush(stdin); system("cls"); } } /**********************************************************************************************/ //uniform double *Uniform_Distribution(unsigned int length, unsigned int num) { unsigned int i = 0; unsigned int xn = (unsigned int)pow(2, 31); unsigned int lamda = (unsigned int)pow(7, 5); unsigned int base = (unsigned int)pow(2, 31) - 1; double *uniform = (double *)malloc(length * sizeof(double)); for (i = 0; i < num; i++) { xn = (lamda * xn) % base; } for (i = 0; i < length; i++) { xn = (lamda * xn) % base; uniform[i] = (double)xn / (double)base; } return uniform; } /**********************************************************************************************/ /**********************************************************************************************/ //demo gaussian void Demo_Gaussian(void) { int i = 0, length = 0; double miu = 0, sigma = 1.0, *gaussian; char filename[50]; FILE *fp; printf("length="); scanf("%d", &length); //length printf("miu="); scanf("%lf", &miu); //miu printf("sigma="); scanf("%lf", &sigma); //sigma sprintf(filename, "Gaussian_%d_%d_%d.txt", (int)(miu * 100), (int)(sigma * 100), length); gaussian = Gaussian_Distribution(miu, sigma, length); fp = fopen(filename,"w"); for (i = 0; i < length; i++) { fprintf(fp, "%lf\n", gaussian[i]);//fscanf(fp, "%lf", &gaussian[i]);//read data } fclose(fp); } /*********************************************/ //Sum-of-12 Method double *Gaussian_Distribution_Standard(int length) { int i = 0, j = 0; double *gaussian = (double *)malloc(length * sizeof(double)); double *(uniform[12]); for (i = 0; i < length; i++) { uniform[i] = Uniform_Distribution(length, (i + 1) * length); } for (j = 0; j < length; j++) { for (i = 0; i < 12; i++) { gaussian[j] = gaussian[j] + uniform[i][j]; } gaussian[j] = gaussian[j] - 6.0; } return gaussian; } //N(miu,sigma) double *Gaussian_Distribution(double miu, double sigma, int length) { int i = 0; double *gaussian_standard = Gaussian_Distribution_Standard(length); double *gaussian = (double *)malloc(length * sizeof(double)); for (i = 0; i < length; i++) { gaussian[i] = gaussian_standard[i] * sqrt(sigma) + miu; } return gaussian; } /**********************************************************************************************/ /**********************************************************************************************/ void Demo_Rayleigh(void) { int i = 0, length = 0; char filename[50];//filename double sigma = 1.0, *rayleigh; FILE *fp; printf("length="); scanf("%d", &length); printf("sigma="); scanf("%lf", &sigma); sprintf(filename, "Rayleigh_%d_%d.txt", (int)(sigma * 100), length); rayleigh = Rayleigh_Distribution(sigma, length); fp = fopen(filename,"w"); for (i = 0; i < length; i++) { fprintf(fp, "%lf\n", rayleigh[i]); } fclose(fp); } //rayleigh double *Rayleigh_Distribution(double sigma, int length) { int i = 0; double *uniform = Uniform_Distribution(length, length); double *rayleigh = (double *)malloc(length * sizeof(double)); for (i = 0; i < length; i++) { rayleigh[i] = sqrt(-log(pow(uniform[i], 2))) * sigma; } return rayleigh; } /**********************************************************************************************/ /**********************************************************************************************/ //demo poisson void Demo_Poisson(void) { int i = 0, length = 0; char filename[50]; double lamda = 0; int *poisson; FILE *fp; printf("length="); scanf("%d", &length); printf("lamda="); scanf("%lf", &lamda); sprintf(filename, "Poisson_%d_%d.txt", (int)(lamda * 100), length); poisson = Poisson_Distribution(lamda, length); fp = fopen(filename,"w"); for (i = 0; i < length; i++) { fprintf(fp, "%d\n", poisson[i]); } fclose(fp); } //poisson int *Poisson_Distribution(double lamda,unsigned int length) { unsigned int i = 0, k = 0; int *poisson = (int *)malloc(length * sizeof(int)); double ans = 1.0, temp = exp(-lamda); srand((unsigned int)NULL); for (i = 0; i < length; i++) { while(ans >= temp) { k++; ans = ans * rand() / 32767.0; } poisson[i] = k - 1; ans = 1.0; k = 0; } return poisson; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-26 00:10:37