Deck Shuffle

This is a discussion on Deck Shuffle within the C++ Programming forums, part of the General Programming Boards category; >> Since you're running the program a few seconds apart, random_shuffle() must use its own random number generator (and not ...

  1. #46
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    >> Since you're running the program a few seconds apart, random_shuffle() must use its own random number generator (and not rand()).


    i was reading up on random_shuffle a little earlier and there are two different usages..

    Code:
    void random_shuffle (RandomAccessIterator first,
                              RandomAccessIterator last,
                              RandomNumberGenerator& rand);
    
    void random_shuffle (RandomAccessIterator first,
                                     RandomAccessIterator last);
    couldnt find anything on RandomNumberGenerator& rand though

    and yes it does not depend on rand being initialized.. as it gives same results if you remove it...

  2. #47
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    also ..

    Code:
    #include <iostream>
    #include <algorithm> //random_shuffle()
    
    using namespace std;
    
    int main()
    {
        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;
    }
    gives this
    Code:
    283467159
    192345876
    916472583
    216895734
    193624857
    945128376
    728134596
    194625783
    279613845
    328956741
    exact copy as if rand is in there ??

  3. #48
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    So srand() doesn't do anything, then? Well then I guess it doesn't use rand().

    couldnt find anything on RandomNumberGenerator& rand though
    I think it's a seed. Pass time(0) to it and see what happens.
    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.

  4. #49
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    Quote Originally Posted by dwks
    I think it's a seed. Pass time(0) to it and see what happens.
    Yeah but thats what I did I the code that I posted and it doesnt work.

  5. #50
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    i passed it like so
    Code:
    random_shuffle(arr, arr+9, time(0));
    it doesnt like it one bit..

    Code:
    /usr/include/c++/3.3.6/bits/stl_algo.h: In function `void
       std::random_shuffle(_RandomAccessIter, _RandomAccessIter,
       _RandomNumberGenerator&) [with _RandomAccessIter = int*,
       _RandomNumberGenerator = time_t]':
    7.cpp:14:   instantiated from here
    /usr/include/c++/3.3.6/bits/stl_algo.h:1673: error: `__rand' cannot be used as
       a function
    7.cpp: In function `int main()':
    7.cpp:14: error: invalid initialization of non-const reference of type 'time_t&
       ' from a temporary of type 'time_t'
    /usr/include/c++/3.3.6/bits/stl_algo.h:1666: error: in passing argument 3 of `
       void std::random_shuffle(_RandomAccessIter, _RandomAccessIter,
       _RandomNumberGenerator&) [with _RandomAccessIter = int*,
       _RandomNumberGenerator = time_t]'

  6. #51
    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.

    [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]
    I tried something exactly like that example that Salem had me try and it worked fine.

  7. #52
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by dwks
    So srand() doesn't do anything, then? Well then I guess it doesn't use rand().

    couldnt find anything on RandomNumberGenerator& rand though
    I think it's a seed. Pass time(0) to it and see what happens.
    Well, it is called RandomNumberGenerator for a reason. You don't have to use the internal random number generator that random_shuffle() uses if you don't want to. You can substitute a user defined function(actually a "functor") that creates the random numbers for you.

    It sounds like it's possible the op's implementation of the STL and random_shuffle() does not use rand() as its internal random number generator, which is not the case with dev c++ or VC++6. They appear to use rand() because when I seed rand() with srand() dev c++ produces a different sequence of numbers each time I run the program (I'm pretty sure that is the same behaviour with VC++6, but I can't test it now.).
    Last edited by 7stud; 01-06-2006 at 05:10 PM.

Page 4 of 4 FirstFirst 1234
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