+ 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?

16th Apr 2017, 9:51 AM
Shreya Parmar
Shreya Parmar - avatar
3 Antworten
+ 9
Um..Can you please include the code from which the question was created?
16th Apr 2017, 11:13 AM
SoraKatadzuma
SoraKatadzuma - avatar
+ 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.
16th Apr 2017, 11:28 AM
SoraKatadzuma
SoraKatadzuma - avatar
+ 1
#include <iostream> #include <cstdlib> using namespace std; int main() { { cout << rand() << endl; } }
16th Apr 2017, 11:18 AM
Shreya Parmar
Shreya Parmar - avatar