+ 2

How do I make a random number in c++

12th Aug 2017, 6:13 AM
waylon
9 Answers
+ 15
#include <iostream> #include <cstdlib> // for rand(), srand() #include <ctime> // for time() int main() { srand(time(0)); // seed built-in RNG with current time std::cout << 1+ rand() % 10; // output random number between 1 to 10 return 0; }
12th Aug 2017, 6:21 AM
Hatsy Rei
Hatsy Rei - avatar
+ 8
Edited original answer. Please refer. 👌
12th Aug 2017, 7:07 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
keep going with the course. this is covered.
12th Aug 2017, 6:20 AM
jay
jay - avatar
+ 7
@waylon Then post your criteria here lol.
12th Aug 2017, 7:02 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand((int)time(0)); int i = 0; while(i++ < 10) { int r = (rand() % 100) + 1; cout << r << " "; } return 0; }
12th Aug 2017, 6:21 AM
KANHAYA TYAGI
KANHAYA TYAGI - avatar
+ 2
thanks @Hatsy Rei but is there a was to make it with in certain criteria
12th Aug 2017, 6:39 AM
waylon
+ 1
1-10 @Hatsy Rei
12th Aug 2017, 7:03 AM
waylon
+ 1
Ok thanks @Hatsy Rei
12th Aug 2017, 7:11 AM
waylon
0
To make a random number use the function "rand()" which is in cstdlib
12th Aug 2017, 6:25 AM
Nikhil Dhaka
Nikhil Dhaka - avatar