0
Hi everyone! How to generate random number in range of 1 to 10 on c++. It should generate different numbers every time
Thank youđđđ
6 Answers
+ 20
search the CPG & modify to your use case.
# below code from sololearn
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
//define the range
int from = 1;
int to = 10;
srand(time(0));
cout << from + (rand() % (to - from + 1));
return 0;
}
links for generating random numbers.
http://www.cplusplus.com/reference/cstdlib/rand/
rand - C++ Reference - Cplusplus.com
http://www.bogotobogo.com/cplusplus/RandomNumbers.php
C++ Tutorial: Random Numbers - 2017 - BogoToBogo
http://www.math.uaa.alaska.edu/~afkjm/csce211/handouts/RandomFunctions.pdf
Random Number Generation C++ It is often useful to generate ...
https://www.daniweb.com/programming/software-development/threads/1769/c-random-numbers
C++ Random Numbers - Software Development | DaniWeb
+ 2
This can be found with just a little research. I have several codes that use this.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
srand(time(0));
cout << (rand() %10);
return 0;
}
+ 2
thank you very much
0
no it should generate only one number. but every time you run the programm it should generate differnt numbers