当前位置:   article > 正文

【算法分析】随机数的生成:均匀分布、正态、泊松 、瑞利_随机数分析

随机数分析

均匀分布:线性同余法

正态分布:sum-of-12 method

其他分布:逆变换法

  1. /*****************************************
  2. Copyright (c) 2015 Jingshuang Hu
  3. @filename:demo.c
  4. @datetime:2015.10.18
  5. @author:HJS
  6. @e-mail:eleftheria@163.com
  7. @blog:http://blog.csdn.net/hujingshuang
  8. *****************************************/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. /**********************************************************************************************/
  13. void menu(void);
  14. double *Uniform_Distribution(unsigned int length, unsigned int num);
  15. void Demo_Gaussian(void);
  16. double *Gaussian_Distribution(double miu, double sigma, int length);
  17. void Demo_Rayleigh(void);
  18. double *Rayleigh_Distribution(double sigma, int length);
  19. void Demo_Poisson(void);
  20. int *Poisson_Distribution(double lamda,unsigned int length);
  21. /**********************************************************************************************/
  22. int main()
  23. {
  24. menu();
  25. return 0;
  26. }
  27. /**********************************************************************************************/
  28. void menu(void)
  29. {
  30. int num = 0, flag = 0;
  31. while(!flag)
  32. {
  33. printf("---------random variates---------\n");
  34. printf("1.gaussian distribution\n2.rayleigh distribution\n3.poisson distribution\n4.exit\n");
  35. printf("---------------------------------\nyour choice<1,2,3,4>:");
  36. scanf("%d", &num);
  37. switch(num)
  38. {
  39. case 1: Demo_Gaussian();break;
  40. case 2: Demo_Rayleigh();break;
  41. case 3: Demo_Poisson(); break;
  42. case 4: flag = 1; break;
  43. default: break;
  44. }
  45. printf("Save success,press any key to continue.\n");
  46. getchar();getchar();
  47. fflush(stdin);
  48. system("cls");
  49. }
  50. }
  51. /**********************************************************************************************/
  52. //uniform
  53. double *Uniform_Distribution(unsigned int length, unsigned int num)
  54. {
  55. unsigned int i = 0;
  56. unsigned int xn = (unsigned int)pow(2, 31);
  57. unsigned int lamda = (unsigned int)pow(7, 5);
  58. unsigned int base = (unsigned int)pow(2, 31) - 1;
  59. double *uniform = (double *)malloc(length * sizeof(double));
  60. for (i = 0; i < num; i++)
  61. {
  62. xn = (lamda * xn) % base;
  63. }
  64. for (i = 0; i < length; i++)
  65. {
  66. xn = (lamda * xn) % base;
  67. uniform[i] = (double)xn / (double)base;
  68. }
  69. return uniform;
  70. }
  71. /**********************************************************************************************/
  72. /**********************************************************************************************/
  73. //demo gaussian
  74. void Demo_Gaussian(void)
  75. {
  76. int i = 0, length = 0;
  77. double miu = 0, sigma = 1.0, *gaussian;
  78. char filename[50];
  79. FILE *fp;
  80. printf("length="); scanf("%d", &length); //length
  81. printf("miu="); scanf("%lf", &miu); //miu
  82. printf("sigma="); scanf("%lf", &sigma); //sigma
  83. sprintf(filename, "Gaussian_%d_%d_%d.txt", (int)(miu * 100), (int)(sigma * 100), length);
  84. gaussian = Gaussian_Distribution(miu, sigma, length);
  85. fp = fopen(filename,"w");
  86. for (i = 0; i < length; i++)
  87. {
  88. fprintf(fp, "%lf\n", gaussian[i]);//fscanf(fp, "%lf", &gaussian[i]);//read data
  89. }
  90. fclose(fp);
  91. }
  92. /*********************************************/
  93. //Sum-of-12 Method
  94. double *Gaussian_Distribution_Standard(int length)
  95. {
  96. int i = 0, j = 0;
  97. double *gaussian = (double *)malloc(length * sizeof(double));
  98. double *(uniform[12]);
  99. for (i = 0; i < length; i++)
  100. {
  101. uniform[i] = Uniform_Distribution(length, (i + 1) * length);
  102. }
  103. for (j = 0; j < length; j++)
  104. {
  105. for (i = 0; i < 12; i++)
  106. {
  107. gaussian[j] = gaussian[j] + uniform[i][j];
  108. }
  109. gaussian[j] = gaussian[j] - 6.0;
  110. }
  111. return gaussian;
  112. }
  113. //N(miu,sigma)
  114. double *Gaussian_Distribution(double miu, double sigma, int length)
  115. {
  116. int i = 0;
  117. double *gaussian_standard = Gaussian_Distribution_Standard(length);
  118. double *gaussian = (double *)malloc(length * sizeof(double));
  119. for (i = 0; i < length; i++)
  120. {
  121. gaussian[i] = gaussian_standard[i] * sqrt(sigma) + miu;
  122. }
  123. return gaussian;
  124. }
  125. /**********************************************************************************************/
  126. /**********************************************************************************************/
  127. void Demo_Rayleigh(void)
  128. {
  129. int i = 0, length = 0;
  130. char filename[50];//filename
  131. double sigma = 1.0, *rayleigh;
  132. FILE *fp;
  133. printf("length="); scanf("%d", &length);
  134. printf("sigma="); scanf("%lf", &sigma);
  135. sprintf(filename, "Rayleigh_%d_%d.txt", (int)(sigma * 100), length);
  136. rayleigh = Rayleigh_Distribution(sigma, length);
  137. fp = fopen(filename,"w");
  138. for (i = 0; i < length; i++)
  139. {
  140. fprintf(fp, "%lf\n", rayleigh[i]);
  141. }
  142. fclose(fp);
  143. }
  144. //rayleigh
  145. double *Rayleigh_Distribution(double sigma, int length)
  146. {
  147. int i = 0;
  148. double *uniform = Uniform_Distribution(length, length);
  149. double *rayleigh = (double *)malloc(length * sizeof(double));
  150. for (i = 0; i < length; i++)
  151. {
  152. rayleigh[i] = sqrt(-log(pow(uniform[i], 2))) * sigma;
  153. }
  154. return rayleigh;
  155. }
  156. /**********************************************************************************************/
  157. /**********************************************************************************************/
  158. //demo poisson
  159. void Demo_Poisson(void)
  160. {
  161. int i = 0, length = 0;
  162. char filename[50];
  163. double lamda = 0;
  164. int *poisson;
  165. FILE *fp;
  166. printf("length="); scanf("%d", &length);
  167. printf("lamda="); scanf("%lf", &lamda);
  168. sprintf(filename, "Poisson_%d_%d.txt", (int)(lamda * 100), length);
  169. poisson = Poisson_Distribution(lamda, length);
  170. fp = fopen(filename,"w");
  171. for (i = 0; i < length; i++)
  172. {
  173. fprintf(fp, "%d\n", poisson[i]);
  174. }
  175. fclose(fp);
  176. }
  177. //poisson
  178. int *Poisson_Distribution(double lamda,unsigned int length)
  179. {
  180. unsigned int i = 0, k = 0;
  181. int *poisson = (int *)malloc(length * sizeof(int));
  182. double ans = 1.0, temp = exp(-lamda);
  183. srand((unsigned int)NULL);
  184. for (i = 0; i < length; i++)
  185. {
  186. while(ans >= temp)
  187. {
  188. k++;
  189. ans = ans * rand() / 32767.0;
  190. }
  191. poisson[i] = k - 1;
  192. ans = 1.0;
  193. k = 0;
  194. }
  195. return poisson;
  196. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/83842
推荐阅读
相关标签
  

闽ICP备14008679号