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😀😀😀

16th Oct 2017, 11:21 AM
Abubakr
Abubakr - avatar
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
16th Oct 2017, 11:40 AM
Lord Krishna
Lord Krishna - avatar
+ 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; }
16th Oct 2017, 1:55 PM
Bob
Bob - avatar
+ 2
thank you very much
16th Oct 2017, 1:56 PM
Abubakr
Abubakr - avatar
0
no it should generate only one number. but every time you run the programm it should generate differnt numbers
16th Oct 2017, 11:31 AM
Abubakr
Abubakr - avatar