Generate random numbers


/ Published in: C
Save to your folder(s)

e.g.
<pre>
$ ./a.out 4 0
1762001.350586 827080.943359 1642278.102539 1674450.112305
</pre>


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.