Why is my code not generating random numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is my code not generating random numbers?

https://code.sololearn.com/c7IFyEGmKF2i/?ref=app When i simply use the rand( ) and srand( ) functions in main( ) function, the program runs just fine. But, when i create a new function by the name of Switch ( ) that has the same functionality, the program doesnt generate random numbers. What is wrong here? Thanks!!

28th Oct 2018, 5:03 AM
Yusha Arif
Yusha Arif - avatar
2 Answers
+ 5
Because you re-seed the RNG with srand() each time you call Switch(). Remove line 22. Seeding the RNG once in your main is enough.
28th Oct 2018, 5:05 AM
Hatsy Rei
Hatsy Rei - avatar
0
Thanks alot Hatsy Rei . Could you also explain how to create a Display( ) function that behaves like a printf. I tried creating the function with if else statements. But the problem i am facing is in returing the output as it is in the form of a string, "RED" or "GREEN" and you cannot return strings in a function.
29th Oct 2018, 2:07 AM
Yusha Arif
Yusha Arif - avatar