only one number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

only one number

Hello guys can explain why this code below says that it will generate random number but i tried coding in microsoft visual studio and also here trying to refresh it again and again but the output is always the same number and its 41 #include <iostream> #include <cstdlib> using namespace std; int main() { cout << rand(); }

7th Apr 2018, 6:52 AM
Joness Labindao
Joness Labindao - avatar
2 Answers
+ 1
rand is just a pseudo-random method. Try using srand() to set a seed value for rand so you get different numbers. For example: #include <time.h> ... srand (time(NULL));
7th Apr 2018, 6:55 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
0
u need to seed srand, for example if seed srand(0), you will get same result next time you run ur cpp using the same seed, use time(0) to seed srand(), time(0) is the seconds in ur system, it will never be the same , so use srand(time(0)) thn rand() will not give same no.s and if u want random number from x to y x+rand()%y
7th Apr 2018, 7:02 AM
Kuldeep Singh
Kuldeep Singh - avatar