Deck Shuffle

This is a discussion on Deck Shuffle within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Salem > well with the way he uses srand(0) Ok, so you can't read then, if you've ...

  1. #31
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    Quote Originally Posted by Salem
    > 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
    Don't try and insult me by saying I can't read. Granted I didn't type it correctly in my previous post, but in the code its right. I copied it straight from what he put.

  2. #32
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Prove it.

  3. #33
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    RETURN VALUE
    On success, the value of time in seconds since the Epoch is returned. On error, ((time_t)-1) is
    returned, and errno is set appropriately.
    Perhaps your time() is broken and it's always returning -1. That would generate the effect you see.
    Investigate this.

  4. #34
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    Quote Originally Posted by 7stud
    Prove it.
    How would you like me to do that? Copy and paste? But then you could just say I copied it twice.

    Salem:
    I will investigate it to see if thats the case.

  5. #35
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    I don't know whats going on, I'm sure the example works as it's suppose to. time() is working fine. I even tried it on another machine. It seems to me that srand() is not working seeing as I get the same numbers. Maybe it's not seeding the generator. And I checked if srand() had a return value and it doesn't so I can't check it.
    Last edited by pjharris; 01-06-2006 at 08:57 AM. Reason: addition

  6. #36
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    So something really basic like
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
          
    int main()
    {
        srand(time(0)); //seed random number generator
        for(int j = 0; j<10; j++)
        {
            cout << rand() << endl;
        }
        cin.get();
        return 0;
    }
    Produces constant output?

    Puzzled...

  7. #37
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    i too was not getting random results when i tested the provided example.. not salem's but the earlier one.. so i was just about to post a little example when salem posted that one.. damn! 1 minute late..

    anyhow.. i still have a little issue with the original code.. here is what i found when i tested it.... ?
    is the array ever initialized.. ? not that i see...
    mine was not working and i did this and now it does..
    i have never messed with random_shuffle.. very good to know about.!

    hth

    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};
        ////    HERE  vvvv
        for(int i=0; i<10; ++i){
            arr[i]=rand();
        }
        for(int j = 0; j<10; j++)
        {
            random_shuffle(arr, arr+9);
    
            for(int i=0; i<9; i++)
            {
                cout<<arr[i]<<endl;;
            }
    
            cout<<endl;
        }
    
        cin.get();
        return 0;
    }
    <edit> oh and btw.. the small example i used resembling salems last post.. works just fine.... </edit>

  8. #38
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    Salem your example works fine for me. It must be random_shuffle() then.

  9. #39
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    nevermind.. i must be a moron.. let me check..

    yes, yes i am

    <edit>
    hmm yes still am..

    but i cant seem to make random_shuffle work either..
    good thing i dont need it..

    oh and yes i am leaving..
    </edit>
    Last edited by xhi; 01-06-2006 at 09:37 AM.

  10. #40
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    is the array ever initialized.. ? not that i see...
    Code:
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    If you aren't familiar with the STL, then the code may seem strange to you, but you have to provide random_shuffle() with the memory range where the values you want shuffled are located.

    How would you like me to do that? Copy and paste? But then you could just say I copied it twice.
    Yes, copy and paste the code your are running. Discussing errors in imaginary code is never very productive.
    Last edited by 7stud; 01-06-2006 at 01:19 PM.

  11. #41
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    yes i know.. i over looked that somehow..

    i do have a question though.. the results i was getting are what threw me off..

    so i run the provided example and get this
    Code:
    283467159
    192345876
    916472583
    216895734
    193624857
    945128376
    728134596
    194625783
    279613845
    328956741
    no matter how many times i run it.. i get the same thing.. i see now what shuffle is doing.. and that the array was initialized.. duh! but i dont understand why the shuffling is the same everytime..

    i would think that it would be different in different program executions also..

    get what im saying??

  12. #42
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    Code:
    #include <iostream>
    #include <algorithm> //random_shuffle()
    #include <ctime> //time()
    #include <cstdlib> //srand()
    using namespace std;
          
    int main() {
        srand(time(0));
        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;
    }
    Thats the code I'm running

    Code:
    [pjharris@rundi]$g++ board.cc -o board
    [pjharris@rundi]$./board;sleep 2;./board
    269543178
    587942631
    893761425
    849256317
    659381274
    583127694
    638412957
    265381497
    947168235
    749263158
    
    269543178
    587942631
    893761425
    849256317
    659381274
    583127694
    638412957
    265381497
    947168235
    749263158
    
    [pjharris@rundi]$
    and thats the output

  13. #43
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You should get the same thing if you run it twice in a second. Try running it a few seconds apart.

    [edit]
    Hey, you beat me to it.

    Since you're running the program a few seconds apart, random_shuffle() must use its own random number generator (and not rand()).

    Try this code:
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    int main(void) {
        srand(time(0));
    
        for(int x = 0; x < 10; x ++) {
            std::cout << rand() % 10;
        }
    
        std::cout << std::endl;
    
        return 0;
    }
    [/edit]
    Last edited by dwks; 01-06-2006 at 01:46 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #44
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    Quote Originally Posted by dwks
    You should get the same thing if you run it twice in a second. Try running it a few seconds apart.
    Thats what the sleep does. Sleep 2 says wait two seconds then run it again And it's not the case about timing, because I ran it 10, 30, 60 mins after and I still got the same. I've been getting the same output for 2 days now.

  15. #45
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I know, see my edit.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 3 of 4 FirstFirst 1234 LastLast
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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21