Random number generator.

I was asked to display two hand of five cards with random cards in each using an array and a 2D array. My problum right now is geting the random genorater to work. thanks for the help and if you see any thing elce that looks wrong I would love to know :)

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    int card1;
    int card2;
    int card3;
    int card4;
    int card5;
    
    int card6;
    int card7;
    int card8;
    int card9;
    int card10;
    
    //2D Array.
    const int card_position = 52;
    const int cardname = 52;
    int deck[card_position][cardname];
    
    //Array for suits.
    string suits[52] = {"two of spades", "three of spades", "four of spades", "five of spades", "six of spades","seven of spade", "eight of spades", "nine of spades", "ten of spades", "jack of spades", "queen of spades", "king of spades", "ace of spades", "two of hearts", "three of hearts", "four of hearts", "five of hearts", "six of hearts", "seven of hearts", "eight of hearts", "nine of hearts", "ten of hearts", "jack of hearts", "queen of hearts", "king of hearts", "ace of hearts", "two of diamonds", "three of diamonds", "four of diamonds", "five of diamonds", "six of diamonds", "seven of diamonds", "eight of diamonds", "nine of diamonds", "ten of diamonds", "jack of diamonds", "queen of diamonds", "king of diamonds", "ace of diamonds", "two of clubs", "three of clubs", "four of clubs", "five of clubs", "six of clubs", "seven of clubs", "eight of clubs", "nine of clubs", "ten of clubs", "jack of clubs", "queen of clubs", "king of clubs", "ace of clubs"};

    //Shuffle the deck.
    srand(time(0));
    
    card_position = rand() % suits;
    
    
    //Deal two hands of five cards
    cout << "Hand 1:\n";
    cout << "-------\n";
    cout << card1 << endl;
    cout << card2 << endl;
    cout << card3 << endl;
    cout << card4 << endl;
    cout << card5 << endl;
    
    cout << "Hand 2:\n";
    cout << "-------\n";
    cout << card6 << endl;
    cout << card7 << endl;
    cout << card8 << endl;
    cout << card9 << endl;
    cout << card10 << endl;
    
    return 0;
}// End Main. 
Last edited on
My problum right now is geting the random genorater to work.


What's the actual problem you're encountering?

Also, you could simplify this in several ways.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <ctime>
#include <string>

int main() {

	std::srand(std::time(static_cast<unsigned int>(0)));

	const unsigned short num_suit_names = 4;
	const unsigned short num_card_names = 13;

	std::string suit_names[num_suit_names] = { "spades", "hearts", "diamonds", "clubs" };
	std::string card_names[num_card_names] = { "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace" };

	unsigned short suit_index = rand() % num_suit_names;
	unsigned short card_index = rand() % num_card_names;

	std::cout << card_names[card_index] << " of " << suit_names[suit_index] << std::endl;

	std::cin.get();
	return 0;
}
Last edited on
Have you even tried to compile this? You have a number of compile errors.

Line 24: Why are you declaring deck as 52x52?

Line 32: You can't assign a number to card_position. card_position is a const. You also can't use an array of strings as the right side of a modulo operator.

Line 38-50: card1-card10 are uninitialized variables (garbage). You need to assign random values to card1 through card10. Be careful. rand()%52 is not guaranteed to return unique values.
Topic archived. No new replies allowed.