Thread: Deck Shuffle

  1. #16
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I find the it interesting that you want to simulate the shuffle of a real dealer.
    But conjoining a split deck rarely ever is a linear one to one swap of cards.
    Usually you get say 0-4 from one side maybe and 0-4 from one side.
    So I just wanted to say it would be interesting if you allowed for this varience in your code.
    What would be really intersting is to create a greater or lesser variance based on the skill level of your bot dealer.

    (me hides for being lazy)

  2. #17
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    I do allow for the variance in my code. Look at my explination of what I wanted it to do when i first posted. Then look at the code. I use rand() to get a number between 1-4, then use that number as the variance. The only thing that isnt real is that the variance is always the sameeach time the deck is shuffled.

    Also, the dealer skill is an interesting idea. But, I want to shuffle the deck as best as possible.

  3. #18
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    My bad, I guess if I wasn't lazy I would have analyzed the code deeper and seen this.
    kudos to you

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I want to shuffle the deck as best as possible.
    As long as you are using rand, a single call to random_shuffle will shuffle the deck as well as possible. Your version is just an interesting simulation of human shuffling, it is not more random (and is potentially less random, I'm not sure).

  5. #20
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    Quote Originally Posted by Daved
    As long as you are using rand, a single call to random_shuffle will shuffle the deck as well as possible. Your version is just an interesting simulation of human shuffling, it is not more random (and is potentially less random, I'm not sure).
    Yeah I was thinking about going with random_shuffle when I found out about it, even tried to use it as you saw in my code(all the other code was commented out when I tried), but that seemed harder because I got that RandomAccessInteger error. But it's also hard for me to throw away that code seeing as I wrote it and tried to debug it for an hour or two maybe more before I posted here.

  6. #21
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As Daved already said:

    Quote Originally Posted by Daved
    >> But that didn't work because the array I pass isnt of type RandomAccessIterator or something.

    Sorry, it should be random_shuffle(d, d+52). If you just want to get a real shuffle, calling that once is all you have to do.
    Use that and it should work!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #22
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    Yeah I think I'll give it a try but I am still undecided about which I want to use. I might use random_shuffle, but it's so hard to just throw away that code I worked on for hours.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I might use random_shuffle, but it's so hard to just throw away that code I worked on for hours.
    Well, you might be throwing that code away (for now), but not what you learnt from writing it and asking for help concerning it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    This is true laserlight. I have learn the valuable lesson of keeping things in the smallest scope possible and when I do loops make sure I reset things that need to be reset.
    Last edited by pjharris; 01-05-2006 at 09:36 AM. Reason: misspelling

  10. #25
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Don't "Throwaway" the code in fact you can improve it further by considering more meaninful names for your variables. This will benifit others reading the code, and ultimately make you a better programmer.

  11. #26
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here's an example using random_shuffle():
    Code:
    #include <iostream>
    #include <algorithm> //random_shuffle()
    #include <ctime> //time()
    #include <cstdlib> //srand()
    
    
    using namespace std;
          
    int main()
    {
        srand(time(0)); //seed random number generator
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        
        for(int j = 0; j<10; j++)
        {
            random_shuffle(arr, arr+9);
            
            for(int i=0; i<9; i++)
            {
                cout<<arr[i];
            }
            
            cout<<endl;
        }
               
        cin.get();
        return 0;
    }
    Last edited by 7stud; 01-05-2006 at 01:26 PM.

  12. #27
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    Quote Originally Posted by 7stud
    Here's an example using random_shuffle():
    Code:
    #include <iostream>
    #include <algorithm> //random_shuffle()
    #include <ctime> //time()
    #include <cstdlib> //srand()
    
    
    using namespace std;
          
    int main()
    {
        srand(time(0)); //seed random number generator
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        
        for(int j = 0; j<10; j++)
        {
            random_shuffle(arr, arr+9);
            
            for(int i=0; i<9; i++)
            {
                cout<<arr[i];
            }
            
            cout<<endl;
        }
               
        cin.get();
        return 0;
    }
    That is all good and dandy. But I just ran your example and I got the same set of numbers each time a ran it, which was like 10 times. So, that basically convinced me not to use it unless someone can help with that.

  13. #28
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Works for me, if I do
    $ ./a.out ; sleep 2 ; ./a.out

    But if I do this, then yes the results are the same, basically because time hasn't gone anywhere and that's what you pass to srand()
    $ ./a.out ; ./a.out

  14. #29
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    well with the way he uses srand(0) what part of time is being used?. And the way I did it was:
    $./a.out
    $./a.out

    and I did that 10 times and it was the same set of numbers. And I just did it again, however many minutes after the fact and bang the same numbers.
    Last edited by pjharris; 01-05-2006 at 02:38 PM. Reason: more to say

  15. #30
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > well with the way he uses srand(0)
    Ok, so you can't read then, if you've got srand(0);
    >> srand(time(0)); //seed random number generator

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shuffle 2-dimensional array
    By patkhor in forum C Programming
    Replies: 5
    Last Post: 11-26-2008, 03:51 PM
  2. Question about engine design.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 01-29-2008, 10:53 AM
  3. Blackjack
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-20-2007, 08:07 PM
  4. Card shuffle and deal want to put on Stack
    By sugie in forum C++ Programming
    Replies: 4
    Last Post: 12-12-2005, 08:40 PM
  5. Replies: 2
    Last Post: 11-07-2003, 12:21 AM