Im stuck with my project. I have no idea how to begin this project.

I need to write a function that shuffles say for example a deck of cards. which is represented by an array of 52 elements.
Every element in the array is a structure for one card like this

typedef struct
{
char *suite; //clubs, diamonds, hearts, spades
int value; // ace 1-9, then jack, queen, king
}
typedef CARD DECK[52]

but i need to use a random number in the range of 0-51 to ensure that each shuffle results in a different card sequence

Recommended Answers

All 6 Replies

Actually what you need to do is create a scheme to represent all the 52 cards with a integer number. It comes to mind mostly easily to have n = s * 13 + v, where n is this integer number, s is [0, 3] each representing a heart/diamond/etc, and v is [0, 13]the value on each card. So now you have [0, 51] representing each card. If you have king, you can number it 52/53.

You get a rand order of 0-51, so for each rand number, say n, you assign round(n, 13) to value and n/13 to suite. To make it simpler, you may predefine a string array, say cardstr[4]={"heart", "diamond", ...}; and assign cardstr[n/13] to suite.

Im stuck with my project. I have no idea how to begin this project.

I need to write a function that shuffles say for example a deck of cards. which is represented by an array of 52 elements.
Every element in the array is a structure for one card like this

typedef struct
{
char *suite; //clubs, diamonds, hearts, spades
int value; // ace 1-9, then jack, queen, king
}
typedef CARD DECK[52]

but i need to use a random number in the range of 0-51 to ensure that each shuffle results in a different card sequence

If you don't want the cards to reapeat, you can create an array of all the possible cards, and do random swaps. Thats the easy way.
If you need speed for some reason, you can do this:
create two arrays: deck (any type) and temp (int). Make sure temp is initialized to null. Generate a random number between 0 and 51. Mark off temp[random_number] to 1 so next time you can skip it. Add the corresponding card to the next free spot in the deck array. Repeat until you finish the deck.
Generating random numbers is vary easy. stdlib.h has two functions called rand() and srand(). rand() will return an integer, but may not be so random unless you seed it. srand() will seed the random number generator, but you need to give it a non constant number. time.h has a function called time() which will return how long the computers been on for (when argument is null). You can use that time to seed the random number generator.
So to generate a random number:

srand((unsinged int)time(NULL));
random_number = rand() % 99;

Good luck.

I found it easier to use a 52 unit bool array, using the index as the unique card ID and the value to determine whether it is in the deck or not. i.e. a[2] would be a 3 of spades, if it is true, it is still in the deck, if it is false, it has already be dealt. As for the hand, you can use the grab the value from the index, depending on how large your hand is, so your card ID is stored in the value.

To sum it up:
Deck=52 unit bool array, index=cardID
Hand=(however many slots), value=cardID

use rand(card)%51 to get numbers in between 0-51!!!

use rand(card)%51 to get numbers in between 0-51!!!

Try again.
155%52 = 51
156%52 = 0
152%51=50
153%51=0.
As you can see, %51 will only ever reach 50 and %52 will only ever reach 51.

commented: True. +18
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.