+ 1
Random no
why is it creating only 41 everytime?? and what if i want to limit the random output from 1 to 100 then how to do?
3 Answers
+ 9
Um..Can you please include the code from which the question was created?
+ 8
It outputs 41 because, funny enough, that's it's default output. To set a range:
rand() % size; //where size is your range.
This will return a number between 0 and size inclusively. I do recommend using srand(time(NULL)); along with is to aid in the randomization. So try something like:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main () {
srand(time(NULL));
cout << rand() % 100 + 1 << endl;
return(0);
}
by adding the one we change the inclusion to 1 and 100, (instead of 0 to 99) like your above request.
+ 1
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
{
cout << rand() << endl;
}
}