C   161
ran int
Guest on 14th March 2023 12:13:54 AM


  1. #define N 100000
  2.  
  3. #include <stdio.h>
  4. #include <strings.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7.  
  8. double random_num()
  9. {
  10.   return (double)rand()/(double)(RAND_MAX);
  11. }
  12.  
  13.  
  14. int main()
  15. {
  16.   int i, j, count, counter[100];
  17.  
  18.   for ( j=0; j<20 ; j++ )
  19.     counter[j] = 0;
  20.  
  21.   for ( i=0; i<N ; i++ )
  22.     {
  23.       j = (int) ( 10.0 * random_num() );
  24.       counter[j]++;
  25.     }
  26.  
  27.   count = 0;
  28.   for ( j=0; j<15 ; j++ )
  29.     {
  30.       printf( "%3d %d\n", j, counter[j] );
  31.       count = count + counter[j];
  32.     }
  33.   printf( " Check - %d\n", count );
  34. }

Raw Paste

Login or Register to edit or fork this paste. It's free.