0

I have a simple program that so far creates 5 community cards (all ints). I've managed to set these (and they are global variables, that's on purpose), but every time I run the program, they create the same number. Below is the code.

int communityCards[5];
int playerCards[2];
int opponentCards[2];
int communityValues[5];
int playerValues[2];
int opponentValues[2];

int main(int argc, const char * argv[]) {
    for (int i = 0; i < 9; i++) {
        if (i < 5) {
            communityCards[i] = rand() % 52 - i;
            for (int a = 0; communityCards[a]!=0; a++) {
                communityCards[i]==communityCards[a]?communityCards[i]++: a = a;
            }
        }
        printf("%i ", communityCards[i]);
    }
}
dunnmifflsys
  • 613
  • 2
  • 9
  • 21
  • just check the manual. you have to call `srand` first. – Karoly Horvath Jun 04 '13 at 23:05
  • 1
    You should start using the newer random generator. http://www.cplusplus.com/reference/random/uniform_real_distribution/uniform_real_distribution/ – dchhetri Jun 04 '13 at 23:09
  • @user814628 - that won't change the behavior of not seeding the generator. The program will still produce the same set of values on every run. – Pete Becker Jun 05 '13 at 01:07

3 Answers3

3

You want to call srand(time(NULL)); once at the beginning of main.

As an aside, however, it looks like the rest of your logic is probably wrong. In particular, it looks like right now you could (for example) deal five aces as the community cards (or is your loop/conditional intended to fix that?)

Usually you'd want to fill a deck, shuffle it with std::random_shuffle, then move cards from there to the community, player's and opponent's hands.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • `time` have 1 second precision, so multiple copies launched at once will produce same result. – SigTerm Jun 04 '13 at 23:20
  • @SigTerm: The C++ standard doesn't specify the precision of `time`. 1 second is common, but it could be a nanosecond or an hour. – Jerry Coffin Jun 04 '13 at 23:29
  • @JerryCoffin, Wow, I've never heard that one before. I haven't run across anyone claiming anything but one second until now. Good to know, thanks. – chris Jun 04 '13 at 23:38
  • It is specifically meant to fix that, I check with the communityValues[i]==communityValues[a]. – dunnmifflsys Jun 05 '13 at 00:01
  • @chris: I should probably also add that although the C standard doesn't specify its precision (or epoch), if memory serves POSIX does specify both (i.e., that `time` returns seconds since midnight, 1 Jan 1970). – Jerry Coffin Jun 05 '13 at 01:29
  • @JerryCoffin, Yeah, I think so, and many implementations follow that. – chris Jun 05 '13 at 01:39
3

Use srand just once with a proper seed before calling rand. Usually, srand(time(0)) is used.

Alternatively, C++11 provides a lot of much better random number generators with fine-grained specifications (rand provides basically no guarantee regarding the quality of the numbers it produces).

syam
  • 14,701
  • 3
  • 41
  • 65
2

write srand func at the beginning of your program It will take current time as seed

int main()
{
    srand(time(0));

    // STUFF
}
Nick
  • 4,192
  • 1
  • 19
  • 30