Ask Experts Questions for FREE Help!
  Advanced
Register  |  Log in  
   Ask    
 Answer  
  Help  

Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
Free Answers in 3 Easy Steps

Register Now
3 Steps

At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.

Home > Computers & Technology > Programming > Compiled Languages > C++   »   using random_shuffle from <algorithm>

 
Question Tools Search this Question Display Modes
Question
 
 
#1  
Old Apr 29, 2007, 09:48 AM
RobotJones
New Member
RobotJones is offline
 
Join Date: Apr 2007
Posts: 4
RobotJones See this member's comment history on his/her Profile page.
using random_shuffle from <algorithm>

I can't seem to get 'random_shuffle' from <algorithm> to work correctly. I have defined a class 'aCard' which has data members 'aRank' and 'aSuit'. Then define a vector of aCards called 'deck'. I initialize the deck with 'initDeck()' and try to shuffle the deck with 'shuffleDeck()'. Just for debugging purposes I have 'showDeck()' to display the current status of 'deck'. (Of course I overloaded << to display a card.) When I call showDeck() after shuffleDeck() it displays the deck with the cards in order as if shuffleDeck() never happened. Here's my code:

-------------------------------------------------------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <vector>
#include "aCard.h" // card module defining aCard, aRank, aSuit, ...

vector<aCard> deck;

void initDeck()
{
aCard card;
deck.clear();
for(aSuit suit=HEARTS; suit<=SPADES; ++suit)
{
for(aRank rank=TWO; rank<=ACE; ++rank)
{
card.setCard(rank,suit);
deck.push_back(card);
}
}
}


void shuffleDeck()
{
random_shuffle(deck.begin(), deck.end());
}


void showDeck()
{
for(vector<aCard>::const_iterator iter=deck.begin(); iter!=deck.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
}


int main()
{
initDeck();
cout << " after initDeck():\n\n";
showDeck();

shuffleDeck();
cout << "\n\n after shuffleDeck():\n\n";
showDeck();

return 0;
}

-------------------------------------------------------------------------------------------------

My output looks like this:

-------------------------------------------------------------------------------------------------
after initDeck():

2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS

after shuffleDeck():

2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS

-------------------------------------------------------------------------------------------------

Why doesn't random_shuffle() work? I was thinking that maybe random_shuffle() DOES work, but it's my showDeck() that has the problem. Either way, can anyone help? Thanks in advance.

Reply With Quote
 
     

Answers
 
 
Old Apr 30, 2007, 06:46 AM   #2  
asterisk_man
Full Member
asterisk_man is offline
 
asterisk_man's Avatar
 
Join Date: Nov 2006
Location: East coast of U.S.A.
Posts: 472
asterisk_man See this member's comment history on his/her Profile page.
Good question. I'm trying to look into your problem. Initially, everything looks good to me. I found an example (Using the random_shuffle() Algorithm to Randomize a Sequence of Elements) that looks like
c Code:
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
  vector<string> vs;
  vs.push_back(string ("Sunday"));
  vs.push_back (string ("Monday"));
  //…fill the rest of the days
  random_shuffle(vs.begin(), vs.end()); /* shuffle days */
  for (int i = 0; i << 7; i++)
    cout<<vs[i]; /* display shuffled elements */
}

The example seems to be identical to what you're doing other than the display of the elements. Just for kicks can you try using deck[i] syntax to output the deck instead of the iterator? Not that I really think that should work but it'll prove that your showDeck() function is good. I'll keep thinking on this one.
  Reply With Quote
 
     


Question Tools Search this Question
Search this Question:

Advanced Search
Display Modes

 
Similar Sponsors

Similar Questions
Question Asker Topic Answers Last Post
edge detection algorithm mihindu Other Programming 2 Mar 27, 2007 08:15 PM
flash 8.0 swf compression algorithm babagau Multimedia 0 Mar 14, 2007 03:04 PM
Sobel algorithm?? yellow_star Internet & the Web 0 Mar 26, 2005 04:13 AM




Copyright ©2003 - 2007, Ask Me Help Desk.
All times are GMT -8. The time now is 07:19 PM.

Content Relevant URLs by vBSEO 3.0.0 RC6 © 2006, Crawlability, Inc.