random numbers

I have two separate functions that I want to create random numbers (1-13) with and I want them to return different numbers. I can't figure out why both functions are returning the same number though, here is the code:
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
int firstCard(){
int card1;
srand((unsigned)time(0));
card1 = (rand() % 13) +1;
switch (card1){
	case 1:
		cout << "A";
		break;
	case 11:
		cout << "J";
		break;
	case 12:
		cout << "Q";
		break;
	case 13:
		cout << "K";
		break;
	}
return card1;
}

int secondCard(){
int card2;
srand((unsigned)time(0));
card2 = (rand() % 13) +1;
switch (card2){
	case 1:
		cout << "A";
		break;
	case 11:
		cout << "J";
		break;
	case 12:
		cout << "Q";
		break;
	case 13:
		cout << "K";
		break;
}
return card2;
}


How can I make them return different numbers each? Also how could I go about returning A, J, Q, K when the case happens instead of 1, 11, 12, or 13?
You're calling srand twice, on lines 3 and 24. You should only ever call it once in the entire program.
If you call srand with the same argument twice, then subsequent calls to rand() will return the same value.
1
2
3
4
srand(123);
printf("%d ", rand() % 13);
srand(123);
printf("%d ", rand() % 13);

In the above code, the two numbers printed are always identical.
1
2
3
srand(123);
printf("%d ", rand() % 13);
printf("%d ", rand() % 13);

In this code, the two numbers printed will probably not be identical.
Oh, alright, thanks. That fixed the random number problem but when I get a result of 11,12,13, or 1 I would like to print J,Q,K,A, respectively. How can I make my program return just "K" instead of "K13"?
How can I make my program return just "K" instead of "K13"?

I'm not sure what you mean by that- currently your program does not return "K13", it prints "K" and then returns 13.

You could change this function so it returns a string, but then you'd have to store all of your cards as strings, which would make things more difficult. You wouldn't be able to easily tell whether one card has a higher rank than another, for example.

I would recommend keeping your function the way it is, but delete the cout bits. If you want to show the user he's got a king, then you can use a function like this:
1
2
3
4
5
6
7
8
std::string string_from_card(int card){ //think of a better function name, though.
 switch(card){
  case 1: return "Ace";
  case 2: return "Two";
  //[...etc]
  case 13: return "King"
 }
}


Then when you want to display the card, you can do this:

1
2
3
//draw a card
int card = firstCard();
std::cout << "you drew this card: " << string_from_card(card);


By the way, notice that the firstCard and secondCard functions are identical. You don't need both if they're the same.
By the way, notice that the firstCard and secondCard functions are identical. You don't need both if they're the same.


Do you mean that I should be able to call as many cards as I'd like and have them all be different random numbers, with just one of these functions?
Yes. Also, what if 'card1' does not equal to any of the case values?
There's no guarantee that you'll get a different random number every time. Theoretically, you might get 100 Aces in a row. If you want to get completely unique values, you'll need to keep track of the numbers that have been previously generated.

Maybe you could create a "deck" object that holds a vector of cards. make a "draw"method that randomly removes one card from the vector, and return it to the user.
Last edited on
here is a card id /shuffler program I wrote recently. the suits are listed every 13 cards until you reach 52, the top array output of each sequence is the 'initial' numerical value of the card, the middle output is where the card is in the deck, and the bottom output is the text representation of the card with its initial numerical value to the right of it . Because the deck is shuffled and stacked, couts can be eliminated and the program can be further developed to encompass any card game you wish, good luck!!

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

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
void shuffle();

int h,i,m;int b=1;int g=1;int w[52][2];int x[52][2]; 


int main()
{
     for (h=1; h<=4;h++)for(i=1;i<=13;i++)
           {
                w[g][1]=i;g=g+1;
           }


     cout << "\n\n";

     for(g=1;g<=52;g++)w[g][2]=g;

     srand (time(NULL) );

     shuffle();


     return 0;
}


void shuffle()

{

     m = rand() % 52 + 1;


     if (w[m][1] ==0)shuffle();

     if(b>=53)return;

cout << "\n\n"; 
   
cout << "w[" <<m<< "][1] = " << w[m][1]<< "  :\n";
x[b][1] =w[m][1]; x[b][2] =w[m][2]; w[m][1]=0;
cout << "x[" <<b<< "][1] = " << x[b][1]<< "  :\n";

if(x[b][2] == 1) cout << "Ace of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 2) cout << "Two of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 3) cout << "Three of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 4)  cout << "Four of Spades: " << x[b][1]<<"\n";
else if(x[b][2] ==  5)  cout << "Five of Spades: " << x[b][1]<<"\n";
else if(x[b][2] ==  6)  cout << "Six of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 7)  cout << "Seven of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 8)  cout << "Eight of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 9)  cout << "Nine of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 10)  cout << "Ten of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 11)  cout << "Jack of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 12)  cout << "Queen of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 13)  cout << "King of Spades: " << x[b][1]<<"\n";
else if(x[b][2] == 14)  cout << "Ace of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 15)  cout << "Two of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 16)  cout << "Three of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 17)  cout << "Four of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 18)  cout << "Five of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 19)  cout << "Six of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 20)  cout << "Seven of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 21)  cout << "Eight of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 22)  cout << "Nine of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 23)  cout << "Ten of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 24)  cout << "Jack of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 25)  cout << "Queen of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 26)  cout << "King of Hearts: " << x[b][1]<<"\n";
else if(x[b][2] == 27)  cout << "Ace of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 28)   cout << "Two of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 29) cout << "Three of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 30)  cout << "four of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 31)   cout << "Five of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 32)  cout << "Six of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 33)  cout << "Seven of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 34)  cout << "Eight of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 35)  cout << "Nine of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 36)  cout << "Ten of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 37)  cout << "Jack of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 38)  cout << "Queen of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 39)  cout << "King of Clubs: " << x[b][1]<<"\n";
else if(x[b][2] == 40)  cout << "Ace of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 41)  cout << "Two of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 42)  cout << "Three of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 43)  cout << "Four of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 44)  cout << "Five of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 45)  cout << "Six of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 46)  cout << "Seven of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 47)  cout << "Eight of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 48)  cout << "Nine of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 49)  cout << "Ten of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 50)  cout << "Jack of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 51)  cout << "Queen Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 52)  cout << "King of Diamonds: " << x[b][1]<<"\n";
else if(x[b][2] == 53)  cout << "jOKER: " << x[b][1]<< "\n";
else if(x[b][2] == 54)  cout << "jOKER: " << x[b][1] << "\n";
     b=b+1;if(b<=52)shuffle(); 
 

}





partial output

..1][1] = 5 :
x[45][1] = 5 :
five of Clubs: 5

w[9][1] = 9 :
x[47][1] = 9 :
nine of Spades: 9

w[2][1] = 2 :
x[48][1] = 2 :
two of Spades: 2

w[21][1] = 8 :
x[49][1] = 8 :
eight of Hearts: 8


w[18][1] = 5 :
x[50][1] = 5 :
five of Hearts: 5


w[50][1] = 11 :
x[51][1] = 11 :
Jack of Diamonds: 11

...

Last edited on
What the #@!#&@!$# is that?
Long if/else chains are wretched.

Lookup tables are your friend.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static const char* cardnames[] = 
{"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};

static const char* suitnames[] = 
{"Spades","Hearts","Clubs","Diamonds"};

string GetCardName(int cardid)
{
  // note card IDs are zero based, not 1 based
  // so 0= Ace of Spades, etc
  // 1 based is unnatural and harder to work with unless 0 has a special meaning
  //   (which it doesn't appear to in this case)

  if( cardid >= 52 )
    return "Joker";

  return string(cardnames[cardid % 13]) + " of " + suitnames[cardid / 13];
}
What the #@!#&@!$# is that?


I lol'ed too but hey this is a beginner forum be nice
Last edited on
We've all been there.

You should've seen some of the code I wrote when I was starting out. Makes that above post look beautiful.
I remember seeing this in my code somewhere:

1
2
3
4
5
6
7
8
unsigned int something = /*...*/;
if(something >= 5) {
   //do stuff
} else if(something < 5) {
   //do something else
} else {
   //do something completely different!?
}
In one of my early RPG attempts, I used a string to identify the name of the maps. IE "Pyramid 4F" or "Town Name" or whatever. Then whenever you took a step, for the collision detection I did something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if(mapname == "Pyramid 4F")
{
  int map[height][width] = { ... /*a bunch of short #defines here to represent different tiles*/ };
  if(map[y][x] != FLOOR_TILE) /* paraphrasing */
    return false;
  else
    return true;
}
else if(mapname == "Town Name")
{
  int map[height][width] = { ... };
  if(map[y][x] ...

...
// repeat for every map in the game 


I eventually had so many maps that my else if chain was too long and the compiler complained saying my program was "too complex". I got around it by ending the else if chain with an else that called a different function with another else if chain.


Oh yeah -- and this was just for collision detection. For the onscreen display I used fully prerendered bitmaps. Didn't draw individual tiles or anything. I just had to make sure the maps I typed up in the above mentioned function happened to match the images I drew.

I really still wish I had that game. It was pretty good considering how horribly it was coded and how much of a novice I was.
LOL yeah,, listen to bcampton! Be Nice lmao ;) actually, throw another tomato and head of lettuce at me!, after the show I can make a nice salad! :D
Last edited on
Christ, the worst part about that code is that the if else et cetera isn't the worst part. Why is everything global? Why does a shuffle function need to be recursive?
did you lose 2 iq points over that helios?, more? lol ill get this stuff as I go along. ive only been at it for,,, a little under 4 weeks now
And don't forget the 2d arrays too.

Sorry, I just expected a shuffle function to be implemented as an explicit loop - like a for
loop or a while loop or something. But there wasn't a single explicit loop in it; just
recursive calls!

(Of course, I _really_ expected a shuffle function to be implemented as a call to
std::random_shuffle(), but that is another story).

EDIT: and the blatant disregard for any kind of formatting standard too...
Last edited on
ouch! that wasnt a tomato! that was an empty beer can! lol :D
Topic archived. No new replies allowed.