Advertisement
War

Programming.

War
Feb 10th, 2016
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.     int random_integer = rand();
  7.     cout << random_integer << endl;
  8. }
  9. The value of RAND_MAX varies between compilers and can be as low as 32767, which would give a range from 0 to 32767 for rand(). To find out the value of RAND_MAX for your compiler run the following small piece of code:
  10.  
  11. #include <cstdlib>
  12. #include <iostream>
  13. using namespace std;
  14. int main()
  15. {
  16.     cout << "The value of RAND_MAX is " <<  RAND_MAX << endl;
  17. }
  18. srand()
  19.  
  20. #include <cstdlib>
  21. #include <ctime>
  22. #include <iostream>
  23. using namespace std;
  24. int main()
  25. {
  26.     srand((unsigned)time(0));
  27.     int random_integer = rand();
  28.     cout << random_integer << endl;
  29. }
  30.  
  31. #include <cstdlib>
  32. #include <ctime>
  33. #include <iostream>
  34. using namespace std;
  35. int main()
  36. {
  37.     srand((unsigned)time(0));
  38.     int random_integer;
  39.     for(int index=0; index<20; index++){
  40.         random_integer = (rand()%10)+1;
  41.         cout << random_integer << endl;
  42.     }
  43. }
  44.  
  45. #include <iostream>
  46. #include <ctime>
  47. #include <cstdlib>
  48. using namespace std;
  49. int main()
  50. {
  51.     srand((unsigned)time(0));
  52.     int random_integer;
  53.     int lowest=1, highest=10;
  54.     int range=(highest-lowest)+1;
  55.     for(int index=0; index<20; index++){
  56.         random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  57.         cout << random_integer << endl;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement