Why the results here are the same? How can I make it generate two diff numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why the results here are the same? How can I make it generate two diff numbers?

https://code.sololearn.com/cyUfv186sp5o/?ref=app

27th Nov 2017, 7:42 AM
Jan Paul Echaveria
Jan Paul Echaveria - avatar
8 Answers
+ 5
Move the srand(time(0)) to the main function, like this: https://code.sololearn.com/czGNvmKkOTs4/?ref=app
27th Nov 2017, 7:47 AM
blackcat1111
blackcat1111 - avatar
+ 4
As to why it is the same, well... we just don't do it that way. šŸ˜‰
27th Nov 2017, 7:50 AM
blackcat1111
blackcat1111 - avatar
+ 4
Well, technically you can, but you may get unexpected and unwanted results (like the equal "random" results just now.) šŸ˜‰
27th Nov 2017, 7:59 AM
blackcat1111
blackcat1111 - avatar
+ 1
Ok, now I know. I thought srand(time(0)) can be put outside main. Thank you šŸ˜€
27th Nov 2017, 7:57 AM
Jan Paul Echaveria
Jan Paul Echaveria - avatar
0
May I know how can I use it on my code?
27th Nov 2017, 11:31 AM
Jan Paul Echaveria
Jan Paul Echaveria - avatar
0
Sure haha. btw thanks
27th Nov 2017, 11:36 AM
Jan Paul Echaveria
Jan Paul Echaveria - avatar
- 1
Use <random> instead of rand(). It was introduced because rand has some issues.
27th Nov 2017, 11:10 AM
Timon PaƟlick
- 1
It's a bit complicated, please look in the internet. They explain it better than me.
27th Nov 2017, 11:35 AM
Timon PaƟlick