Rand Creates the Same Random Number Over and Over Again
rand() always gives same number
I just started learning c++ and I was trying to recreate an erstwhile blackjack program I had made in high school to examination what I have learned in my grade.
Anyway, every fourth dimension i use rand() i get the same numbers every single time I run the program. IE, the first "hand" that is played is always [ vii, eleven ]. How tin can I make information technology so that the generated numbers are random every time?
This lawmaking is not complete for a blackjack program, but I demand to overcome this rand() issue before I motion on to other calculations. It currently compiles and works as is without whatever other issues.
Thank you so much in advance.
| |
You oasis't seeded the C random number generator by calling Once srand()
;
Add the following line before you enter your while
loop:
srand(time(Zilch));
Y'all will likewise need to include the <ctime>
header.
I would propose y'all get away from using the C pseudo random number generator and look into the C++ library <random>
.
The C standard recommends not using rand/srand if there are improve ways to generate random numbers available.
Last edited on
You lot need to seed it.
| |
For example.
| |
This is just an instance of the numerous methods to obtain random value.
| |
Last edited on
Thanks then much! These were both very helpful!
Topic archived. No new replies allowed.
Source: http://www.cplusplus.com/forum/beginner/230838/
0 Response to "Rand Creates the Same Random Number Over and Over Again"
Post a Comment