+ 1

How to print random number in c++ ??

5th Dec 2020, 4:04 AM
Vansh Sharma
Vansh Sharma - avatar
6 odpowiedzi
+ 10
Vansh.py See to print a random number we can use rand() method! Now if we want a number in range 0 to max.. Am using modulus operator in example to get the remainder. For the seed value am using the time(0) function result into the srand() function... Example - #include<iostream> #include<cstdlib> #include using namespace std; main() { int max; max = 100; //set the upper bound to generate the random number srand(time(0)); cout << "The random number is: "<<rand()%max; }
5th Dec 2020, 4:30 AM
Piyush
Piyush - avatar
+ 6
U can use rand() function
5th Dec 2020, 4:10 AM
Sâñtôsh
Sâñtôsh - avatar
+ 4
rand() use kro bhai
5th Dec 2020, 12:09 PM
Rishabh Singh
Rishabh Singh - avatar
+ 2
I'd like to add the newer C++11 way to do things, using the <random> header: https://code.sololearn.com/c4cRT96vlItJ/?ref=app `std::mt19937` is the method of generating new numbers and with `std::uniforn_int_distribution` you can say which range of numbers you want to generate (in this case, from 0 to 20).
5th Dec 2020, 11:02 AM
Schindlabua
Schindlabua - avatar
+ 2
Use rand() //Don't forget to include time.h
5th Dec 2020, 11:26 AM
SuDeep Jaiswal