Posted By

mustam on 08/19/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

cdnicoll


Generate random numbers


 / Published in: C
 

e.g.

$ ./a.out 4 0
1762001.350586 827080.943359 1642278.102539 1674450.112305

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define USAGE "Usage: %s NUMBER SEED\n"
  5.  
  6. int main(int argc, char ** argv)
  7. {
  8. int i, n, seed;
  9.  
  10. if(argc!=3){
  11. fprintf(stderr, USAGE, argv[0]);
  12. exit(0);
  13. }
  14. n=atoi(argv[1]);
  15. seed=atoi(argv[2]);
  16. srand(seed);
  17. for(i=0; i<n; i++) printf("%.3lf ", (double)rand()/1024);
  18.  
  19. return 0;
  20. }

Report this snippet  

You need to login to post a comment.