Advertisement
cd62131

Histogram

Jul 23rd, 2014
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. double uniform_rand(void) {
  4.   return rand() / ((double) RAND_MAX + 1.);
  5. }
  6. double normal_rand_sub(void) {
  7.   double z = 0.;
  8.   int i;
  9.   for (i = 0; i < 12; i++)
  10.     z += uniform_rand();
  11.   return z - 6.;
  12. }
  13. double normal_rand(double ave, double sd) {
  14.   return sd * normal_rand_sub() + ave;
  15. }
  16. int main(void) {
  17.   const int section = 101;
  18.   int i, j, n = 3000, spec, freq[section];
  19.   double ave = 50., sd = 10.;
  20.   for (i = 0; i < section; i++)
  21.     freq[i] = 0;
  22.   for(i = 0; i < n; i++) {
  23.     spec = (int) normal_rand(ave, sd);
  24.     if (spec < 0) freq[0]++;
  25.     else if (spec > section - 1) freq[section - 1]++;
  26.     else freq[spec]++;
  27.   }
  28.   for (i = 0; i < section; i++) {
  29.     printf("%3d - ", i);
  30.     for (j = 0; j < freq[i]; j++)
  31.       printf("*");
  32.     puts("");
  33.   }
  34.   return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement