Random in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Random in c++

hello im trying to generate a random number from 0-3 but i cant seem to get it to work, i already tried rand() but i need to generate it like 50 so that isnt gonna work, does someone know a good solution?

26th Aug 2019, 2:42 PM
Cat Sauce
Cat Sauce - avatar
7 Answers
+ 1
Cat Sauce It doesn't generate 1 number, you just have to add endl cout << rand() % 4 << endl; If you get the same output on each run, add srand(time(0)); before the loop. Don't forget to include ctime header
26th Aug 2019, 3:02 PM
Agent_I
Agent_I - avatar
0
So you want to generate 50 random numbers with the range from 0 - 3? You can use for loop with rand function inside it
26th Aug 2019, 2:50 PM
Agent_I
Agent_I - avatar
0
Agent_I i made a for loop but it only give me 1 number so 3333333.........
26th Aug 2019, 2:54 PM
Cat Sauce
Cat Sauce - avatar
0
Cat Sauce Can you show me how you do it?
26th Aug 2019, 2:55 PM
Agent_I
Agent_I - avatar
0
Agent_I for(int i = 0; i < 50;i++){ cout << rand() % 4; }
26th Aug 2019, 2:57 PM
Cat Sauce
Cat Sauce - avatar
0
~ swim ~ i didnt understand it
26th Aug 2019, 3:48 PM
Cat Sauce
Cat Sauce - avatar
26th Aug 2019, 5:44 PM
Cat Sauce
Cat Sauce - avatar