April 25, 2017, 01:47:51 AM*

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
Advanced search  
Pages: [1]
Author Topic: Pack of cards?  (Read 1249 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline

Posts: 17

« on: June 30, 2015, 08:07:35 AM »

I think I've asked this before, but I'll throw it out again and see what happens....

Ok, imagine you're writing a program to shuffle a pack of cards.

Let's say the cards are simply numbered 1-52 and you have a program further down the line to convert an integer X to a face value and suit given X:52>=X>=1.

You have a standard random number generator, let's say


Which will return a random integer between 0 and X

You can have as many arrays as you want, say:

Array deck = int[52]

Which will produce an array "deck" with elements from 0-51.

You can index into the array with .variable e.g:

int deal = deck.cardnumber

Makes sense?

Ok, now write a piece of pseudo code describing an algorithm which will deal a shuffled deck of cards.

I noticed you wrote a card game, so you'll be good at this! It took me quite a long time to think of something you'd be good at. :-)

Ok, off you go then.... anybody that is. Programmers, non programmers.....

The question is: How do you write a program which deals a shuffled deck of cards?
« Last Edit: June 30, 2015, 08:09:31 AM by vapourmile » Logged
RBP Member
Offline Offline

Gender: Male
Posts: 462

"Ow!!! What's that ?!?!"

« Reply #1 on: June 30, 2015, 12:15:10 PM »

I would swap two random elements of the card array say 100.000 times
Blue Gene Super Computer
Offline Offline

Gender: Male
Posts: 7757

« Reply #2 on: June 30, 2015, 06:20:31 PM »

This is how we do it in WinSol

// This will shuffle the deck into a random order
for (int i=0; i<(m_deck.size()-1); i++)
// Get a random position for this card
int r = i + (rand() % (m_deck.size()-i));

swap(m_deck[i], m_deck[r]);
RBP Member
Offline Offline

Gender: Male
Posts: 661

« Reply #3 on: June 30, 2015, 10:01:42 PM »

I would do it like this;

1. Get random nunber between 2 and 52.
2. Move that card in the deck to the top of the deck.
3. Repeat lots of times.

Done. Smiley
Pages: [1]
Jump to: