+ 2

Srand

#include <iostream> #include <cstdlib> using namespace std; int main () { srand(98); { cout << 1 + (rand() % 6) << endl; } } why here it's outputting only 5 everytime?

16th Apr 2017, 10:03 AM
Shreya Parmar
Shreya Parmar - avatar
7 odpowiedzi
+ 4
Add #include <ctime> to header rows Modify seeder as srand ( time (0) )
16th Apr 2017, 10:33 AM
Babak
Babak - avatar
+ 1
Ohk
16th Apr 2017, 10:13 AM
Shreya Parmar
Shreya Parmar - avatar
+ 1
then what's the use of using this code which i have mentioned in the question... because it outputs the same number always
16th Apr 2017, 10:14 AM
Shreya Parmar
Shreya Parmar - avatar
+ 1
With regards to your other question on how to get a random number between 1 - 100, this code is almost there. Just replace % 6 with % 101
16th Apr 2017, 10:15 AM
Nicole Benadè
Nicole Benadè - avatar
0
include the correct header file <time>. Then use srand(time()). But it will only output 1 - 5 because of the %6
16th Apr 2017, 10:12 AM
Nicole Benadè
Nicole Benadè - avatar
0
I suspect it has something to do with your seed. You only allow 1 - 5, but "start" with 98
16th Apr 2017, 10:20 AM
Nicole Benadè
Nicole Benadè - avatar