Search This Blog

Saturday, December 31, 2011

rand() examples in c++

C++ Syntax (Toggle Plain Text)
  1. #include
  2. #include
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int random_integer = rand();
  9. cout << random_integer << endl;
  10. }
     
     
    C++ Syntax (Toggle Plain Text)
    1. #include
    2. #include
    3.  
    4. using namespace std;
    5.  
    6. int main()
    7. {
    8. cout << "The value of RAND_MAX is " << RAND_MAX << endl;
    9. }
       
       
       
      C++ Syntax (Toggle Plain Text)
      1. #include
      2. #include
      3. #include
      4.  
      5. using namespace std;
      6.  
      7. int main()
      8. {
      9. srand((unsigned)time(0));
      10. int random_integer = rand();
      11. cout << random_integer << endl;
      12. }
         
         
         
         
        C++ Syntax (Toggle Plain Text)
        1. #include
        2. #include
        3. #include
        4.  
        5. using namespace std;
        6.  
        7. int main()
        8. {
        9. srand((unsigned)time(0));
        10. int random_integer;
        11. for(int index=0; index<20; index++){
        12. random_integer = (rand()%10)+1;
        13. cout << random_integer << endl;
        14. }
        15. }
           
           
           
          C++ Syntax (Toggle Plain Text)
          1. #include
          2. #include
          3. #include
          4.  
          5. using namespace std;
          6.  
          7. int main()
          8. {
          9. srand((unsigned)time(0));
          10. int random_integer;
          11. int lowest=1, highest=10;
          12. int range=(highest-lowest)+1;
          13. for(int index=0; index<20; index++){
          14. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
          15. cout << random_integer << endl;
          16. }
          17. }
             
             
             
             
             
           
         
       
     

No comments:

Post a Comment

Thank you