Advertisement
luizaspan

[CANZIAN - C] Random (histograma)

Nov 13th, 2015
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. #define n 10 // hst.length = n
  6. #define N 10000
  7.  
  8. int main(void)
  9. {
  10.     double hst[n]={0};
  11.     double dx = (double) 1/n;
  12.     double x;
  13.     int i,j;
  14.  
  15.     // for (j=0; j<n; j++)
  16.     //  hst[j]=0;
  17.  
  18.     for (i=0; i<N; i++) {
  19.         x = sqrt((double) rand()/RAND_MAX);
  20.         // printf("%lf\n",x);
  21.         for (j=0; j<n; j++) {
  22.             if (x>=j*dx && x<(j+1)*dx) {
  23.                 hst[j]++;
  24.             }
  25.         }
  26.     }
  27.  
  28.     // printf("\n");
  29.  
  30.     for (j=0; j<n; j++) {
  31.         printf("%lf \n",hst[j]);
  32.     }
  33.  
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement