C++ random number generator


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



Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. int main(int argc, char *argv[]){
  6. if(argc != 3){
  7. cout << "Usage: " << endl;
  8. cout << " rand [int x] [int y]" << endl;
  9. cout << " [x] = highest possible value of a random number" << endl;
  10. cout << " [y] = quantity of random numbers created" << endl << endl;
  11. exit(1);
  12. }
  13. long int M = atoi(argv[1]);
  14. int quantity = atoi(argv[2]);
  15. srand(time(NULL));
  16. for(int count = 1; count <= quantity; ++count)
  17. cout << (((int)(((double)rand()/(double)(RAND_MAX+1))*M))*-1)+1 << endl;
  18. return 0;
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.