We Recommend

C++ The Core Language C++ The Core Language
C++: The Core Language is for C programmers transitioning to C++. It's designed to get readers up to speed quickly by covering an essential subset of the language. The subset consists of features without which it's just not C++, and a handful of others that make it a reasonably useful language.


Posted By

gdonald on 09/27/06


Tagged

number random generator


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

copyleft


C++ random number generator


Published in: C++ 


  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 

You need to login to post a comment.