CPP   114

random

Guest on 20th April 2022 02:13:21 AM

  1. #include <iostream>
  2. // The c standard library, math and time libraries are needed.
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <ctime>
  6. using namespace std;
  7. // Pick a random number from 1 to 10.
  8. int main() {
  9.         // Random number picked.
  10.         int random_number=0;
  11.  
  12.         // Maximum random number to pick.
  13.         int max_random=10;
  14.  
  15.         // Set the randomization seed to be based on the current time in seconds in Jan 1, 1970.
  16.         srand((unsigned)time(0));
  17.  
  18.         // Pick a random number from 1 to max_random.
  19.         random_number=rand()%max_random+1;
  20.  
  21.         cout << "Random number picked: " << random_number << endl;
  22.         return 0;
  23. }

Raw Paste


Login or Register to edit or fork this paste. It's free.