rand() not working

May 25, 2013 at 7:34pm
Hi, I'm trying to make a program that runs a weird version of poker. I'm trying to randomly assign cards to each player and community cards (the flop, river, and other one, the things everyone can use), but it's not working. No matter what, the values are the queen, the 5, the 6, the jack, and the 4. I would appreciate anything, wether it's an idea of where the bug may be, something that would make it work better that has nothing to do with the bug, or a fix to the bug itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  //AdventureRPG

//Declarations of global variables (I know they have consequences, but I want to have those consequences) and Inclusions
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define size 4
using namespace std;
char response[size];
string cardValues[14] = {"You'll never see me in the program!", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

//Finds the value (Ace-King) of the cards
int getValue(int a) {
    --a;
    int z = a % 13;
    z++;
    return z;
}

//Finds the suit and returns it
string getSuit(int a) {
    string suitOut;
    int z = a % 13;
    int x = a-z;
    x /= 13;
    if (x==0) {
        suitOut="Diamonds";
    } else if (x==1) {
        suitOut="Hearts";
    } else if (x==2) {
        suitOut="Spades";
    } else if (x==3) {
        suitOut="Clubs";
    }
    return suitOut;
}

//When given 7 cards (your cards and community cards), checks for largest pair
int checkPair(int a, int b, int c, int d, int e, int f, int g) {
    int value = 0;
    int array[5] = {c, d, e, f, g};
    for (int i=0; i < 5; i++) {
        if (a==array[i]) {
            (a==1) ? a = 14 : 0;
            (value < a) ? value = a : 0;
        } else if (b==array[i]) {
            (b==1) ? b = 14 : 0;
            (value < b) ? value = b : 0;
        }
    }
    if (a==b) {
        (a==1) ? a = 14: a = a;
        a = value;
    }
    return value;
}

int main(int argc, const char * argv[]) {
    //Declares community cards and aa-ae
    int communityCards[5];
    int aa;
    int ab;
    int ac;
    int ad;
    int ae;
    
    
    //Assigns aa to random number, set communityCards[0] to it
    aa = rand() % 52 + 1;
    communityCards[0] = aa;
    
    while (true) {
        //Sets a random number
        ab = rand() %52 + 1;
        //Checks if ab = aa (can't have same card twice), if not, breaks while statement.
        if (ab!=aa) {
            break;
        }
    }
    //Sets community card to ab
    communityCards[1] = ab;
    while (true) {
        ac = rand() %52 + 1;
        if (ac!=aa && ac!=ab) {
            break;
        }
    }
    communityCards[2] = ac;
    while (true) {
        ad = rand() %52 + 1;
        if (ad!=aa && ad!=ab && ad!=ac) {
            break;
        }
    }
    communityCards[3] = ad;
    while (true) {
        ae = rand() %52 + 1;
        if (ae!=aa && ae!=ab && ae!=ac && ae!=ad) {
            break;
        }
    }
    communityCards[4] = ae;
    
    int communityValues[5];
    //Goes through all community cards, gets the value of them.
    for (int i = 0; i < 5 ; i++) {
        communityValues[i]=getValue(communityCards[i]);
    }
    //Sets the users cards, gets the value of them.
    int ba = 25;
    int bc = 16;
    int z = getValue(ba);
    int w = getValue(bc);
    
    //Prints all of the cards
    printf("The community cards are the %s, the %s, the %s, the %s, and the %s. Your cards are the %s, and the %s. ", cardValues[communityValues[0]].c_str(), cardValues[communityValues[1]].c_str(), cardValues[communityValues[2]].c_str(), cardValues[communityValues[3]].c_str(), cardValues[communityValues[4]].c_str(), cardValues[z].c_str(), cardValues[w].c_str());
    
    //Checks to find any pairs
    int x = checkPair(z, w, communityValues[0], communityValues[1], communityValues[2], communityValues[3], communityValues[4]);
    string b = cardValues[w];
    string a = cardValues[z];
    (x == 0) ? printf("You don't have a pair.") : printf("You have a pair of %s's.", cardValues[x].c_str());
    return 0;
}


Thank you again.
May 25, 2013 at 7:38pm
add std::srand(std::time(0)); at the beginning of main()
And #include <ctime>
Also it is <cstdio> and <cstdlib>; <stdio.h> and <stdlib.h> are outdated and should not be used
Topic archived. No new replies allowed.