+ 1
How to print random number in c++ ??
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;
}
+ 6
U can use rand() function
+ 4
rand() use kro bhai
+ 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).
+ 2
Use rand()
//Don't forget to include time.h