Generate a random number inside a specific range.
 
To randomly generate a number, we first need the function rand() to generate the number. Becuase this number doesn't change after the application is compiled, we have srand() to change the number. We can use srand() to generate a time-based number which changes every second.
#include<iostream>
#include<cstdlib>
#include <time.h>
using namespace std;
int main() {
srand(time(NULL));
int randominteger = rand()%100;
cout<<randominteger<<"n"
//cout<<RAND_MAX<<"n";
return 0;
}

Remember to include cstdlib for the rand functions and time.h for the time() function!
This application above is an example to create a random number between 0 and 100. Normally, a random number is defined like this: int randnumb = rand();
But this puts out a number between 0 and RAND_MAX where RAND_MAX is the maximum reachable number for rand, which is available in your the compiler.
If you want to know what the maximum of RAND_MAX is on your computer, yust output the RAND_MAX variable "cout << RAND_MAX << endl;".
If you want to definy your own range for rand, the easyest way is the above:
int number = rand()%100; //<-- Puts out a number between 0 and 100.
 
admin / Nov 01, 2003
   
 
 
Login: 
Pass: 
 
 
     
     
2004 - 2019 / lookass.ch
makememad@lookass.ch