#Random is not Random

14 messages · Page 1 of 1 (latest)

radiant glacier
#

Hey, I wrote a small random function, which creates a value for poker cards. But if i use the funktion two times (card1 = kartenZufall; card2 = kartenZufall), it gives me the same value for both cards. And if do it step by step using a stop point, it delivers different values. Wtf is going on

covert flowerBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

radiant glacier
#

int kartenZufall()
{
int karte = 0;
srand((unsigned)time(NULL));

karte = (rand() % 13) + 2;

if (karte > 10)
{
    if (karte == 14)
    {
        karte = 11;
    }
    else
    {
        karte = 10;
    }
}
return karte;

}

real sorrel
#

Random is never purely random, you need to seed it with delta or system time

half plaza
#

You should only srand (seed the random number) once at the beginning of the program

#

You're doing it at every function call here, which will keep reseeding it with the same value because I assume you call this function multiple times within a second

radiant glacier
#

ok yes i need to write srand % 13

half plaza
#

No, move the srand call to the top of main

half plaza
radiant glacier
#

ok

real sorrel
#

I maybe should have been a bit more clear doing it more as once

radiant glacier
#

Ok thank you it worked...

#

!solved