vendredi 17 février 2017

C++ rand and srand [duplicate]

This question already has an answer here:

I am a student learning C++ and am having some trouble with rand () and srand (time(0)). I am writing a program that plays a probability game one million times and returns the number of times a certain result was reached.

Right now I've set a separate function that is supposed to generate a random bool that looks like this:

bool randomBool ()
{
srand (time(0));
return rand () %2 == 1;
}

However, this program is slower than a snail. I haven't even let it run the whole time because it is painfully slow. When I remove the srand (time(0)), The program is much quicker but it gives me the same results every time.

Is there any way to seed the rand() without it taking forever? Thanks!




Aucun commentaire:

Enregistrer un commentaire