War icon

Programming.

War | PRO | 02/10/16 11:24:03 PM UTC | 0 ⭐ | 641 👁️ | Never ⏰ | []
C++ |

1.4 KB

|

None

|

0 👍

/

0 👎

#include <cstdlib> 
#include <iostream>
using namespace std;
int main() 
{ 
    int random_integer = rand(); 
    cout << random_integer << endl; 
}
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:
 
#include <cstdlib> 
#include <iostream>
using namespace std;
int main() 
{ 
    cout << "The value of RAND_MAX is " <<  RAND_MAX << endl; 
}
srand()
 
#include <cstdlib> 
#include <ctime> 
#include <iostream>
using namespace std;
int main() 
{ 
    srand((unsigned)time(0)); 
    int random_integer = rand(); 
    cout << random_integer << endl; 
}
 
#include <cstdlib> 
#include <ctime> 
#include <iostream>
using namespace std;
int main() 
{ 
    srand((unsigned)time(0)); 
    int random_integer; 
    for(int index=0; index<20; index++){ 
        random_integer = (rand()%10)+1; 
        cout << random_integer << endl; 
    } 
}
 
#include <iostream> 
#include <ctime> 
#include <cstdlib>
using namespace std;
int main() 
{ 
    srand((unsigned)time(0)); 
    int random_integer; 
    int lowest=1, highest=10; 
    int range=(highest-lowest)+1; 
    for(int index=0; index<20; index++){ 
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
        cout << random_integer << endl; 
    } 
}

Comments