How do I make a code set radom number values to a int variable C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How do I make a code set radom number values to a int variable C++

Random numbers

23rd Mar 2018, 1:27 AM
Nicholas Marcello
Nicholas Marcello - avatar
11 Answers
+ 3
cout << "random number = " << rand() << endl; int x = rand(); rand() % 10 + 1; // Numbers from 1 to 10
23rd Mar 2018, 1:35 AM
Aidos Zhakupov
Aidos Zhakupov - avatar
+ 3
It really helped you or there are still some questions?
23rd Mar 2018, 1:38 AM
Aidos Zhakupov
Aidos Zhakupov - avatar
+ 2
wait It is continously always the same random number why wont it change?
23rd Mar 2018, 1:46 AM
Nicholas Marcello
Nicholas Marcello - avatar
+ 1
Your the best
23rd Mar 2018, 1:35 AM
Nicholas Marcello
Nicholas Marcello - avatar
+ 1
yeah I forgot the rand() part
23rd Mar 2018, 1:39 AM
Nicholas Marcello
Nicholas Marcello - avatar
+ 1
At last it works. Glad) #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() { int m; srand(time(NULL)); for(int i = 0; i < 10; i++) { m = 10 + rand() % 100; cout << m << endl; } return 0; }
23rd Mar 2018, 2:14 AM
Aidos Zhakupov
Aidos Zhakupov - avatar
+ 1
thank the code is running perfetly , aldo I dont fuly understand how the time element work
23rd Mar 2018, 2:17 AM
Nicholas Marcello
Nicholas Marcello - avatar
+ 1
I wil research it.
23rd Mar 2018, 2:20 AM
Nicholas Marcello
Nicholas Marcello - avatar
0
Thank you
23rd Mar 2018, 1:35 AM
Nicholas Marcello
Nicholas Marcello - avatar
0
oh so time is also an effect that is used to change the number.
23rd Mar 2018, 2:10 AM
Nicholas Marcello
Nicholas Marcello - avatar
0
I will research it
24th Mar 2018, 7:43 PM
Aisha Alausa