Randomly shuffling an array a certain number of times then queue-ing

This is a discussion on Randomly shuffling an array a certain number of times then queue-ing within the C++ Programming forums, part of the General Programming Boards category; I'm a new C++ programmer, and wanted to do the following: 1. Fill a 1D array with values; 2. randomly_shuffle ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    10

    Randomly shuffling an array a certain number of times then queue-ing

    I'm a new C++ programmer, and wanted to do the following:

    1. Fill a 1D array with values;
    2. randomly_shuffle those values...
    3. a number of times, which I'll get via cin (x number of times, an int)
    4. and then, put all of those shufflings back to back. so, for example:

    if the array was 1, 2, 3, 4, 5

    and I chose "4" as the number of times, the output would be

    4 5 1 2 3 3 2 1 5 4 5 4 3 2 1 2 1 3 5 4

    (no, this is not homework I'm asking anyone to do, just trying to develop a quick and easy bootstrap program for my own use.)

    Thanks sincerely for any help -

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Okay. You interestingly put an _ in step 2, so I suppose you know about random_shuffle. You can use a loop to put a bunch of elements into a vector a bunch of times.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    10
    Haven't got to vectors yet. Will probably run into that in the fall.

    What would I change, here, to put in any old set of numbers? Let's say I wanted to put the following 5 element 1D array in: 2, 4, 6, 8, 10

    Code:
    #include <iostream>
    #include <algorithm>
    #include <cassert>
    #include <functional>  
    using namespace std;
    
    int main() 
    {
    
      int a[100];
      int i,t;
      for (i = 0; i < 100; ++i) 
        a[i] = i;
    
      random_shuffle(&a[0], &a[100]);
      for(t = 1; t <= 10; t++)          // 100 times (10,000 random number shuffled)
      {
            for (i = 0; i < 100; ++i) //first, generate 100 random numbers...
            cout <<  a[i] << " ";
      }
      system("pause");
      return 0;
      
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then set a[i] to be something other than i, if you don't want 0, 1, 2, 3, 4.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    10
    I don't follow. Sorry. Like I said, pretty new at this.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you see where the values of a[i] are being set?

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    10
    Yes. Using the example of 2, 4, 6, 8, 10 something like:

    Code:
    int main() 
    {
    
      int a[5] = {2, 4, 6, 8, 10};
      int i,t;
      a[i] = i;
    
      random_shuffle(&a[0], &a[5]);
      for(t = 1; t <= 10; t++)          // 100 times (10,000 random number shuffled)
      {
            for (i = 0; i < 100; ++i) //first, generate 100 random numbers...
            cout <<  a[i] << " ";
      }
      system("pause");
      return 0;
    }
    ?

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    10
    Whoops. Not that. Disregard.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    10
    Trying again:

    Code:
    int main() 
    {
    
      int a[5] = {2, 4, 6, 8, 10};
      int i,t;
      //for (i = 0; i < 100; ++i) 
      a[i] = i;
    
      random_shuffle(&a[0], &a[5]);
      for(t = 1; t <= 10; t++)          // 100 times (10,000 random number shuffled)
      {
            //for (i = 0; i < 100; ++i) //first, generate 100 random numbers...
            cout <<  a[i] << " ";
      }
      system("pause");
      return 0;
      
    }
    That compiles, but crashes when I try to run it -

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    You should have nested loops. The inner loop would just print the elements of the array. The outer loop would loop for the desired number of times, and then shuffle the array before control enters the inner loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    10
    This gets me a little closer, but it looks like I'm generating some sort of weird data. It compiles and runs...oddly.

    Code:
    #include <iostream>
    #include <algorithm>
    #include <cassert>
    #include <functional>  
    using namespace std;
    
    int main() 
    {
    
      int a[5] = {2, 4, 6, 8, 10};
      int i,t;
      a[5] = i;
    
      for(t = 1; t <= 10; t++)
      {
          random_shuffle(&a[0], &a[5]);
          
          for(i = 1; i <= 5; i++)        
          {
                cout <<  a[i] << " ";
          }
      }
    
    system("pause");
    return 0;
      
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    This is wrong:
    Code:
    a[5] = i;
    The array a has 5 elements, so a[5] does not exist. Even if it did, it does not make sense to assign i to it since i was not initialised.

    This is wrong:
    Code:
    for(i = 1; i <= 5; i++)
    again, because a only has 5 elements, i <= 5 should be i < 5. i should start from 0.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    10
    That seems to work, thanks!

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    10
    Something I'd like to do next is to create a new array and populate it with the outcome of this sequence. So, if I had an initial array of 5 elements, and shuffled it randomly 4 times, I'd have a resulting sequence of 20 numbers. Can you guys give me an idea of how to add the outcome of this random shuffle being done x number of times to a new array?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    Instead of printing to standard output, push_back to a std::vector<int>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to run a loop a set number of times?
    By 777funk in forum C Programming
    Replies: 3
    Last Post: 11-04-2010, 10:01 PM
  2. Replies: 25
    Last Post: 10-31-2009, 01:45 AM
  3. Replies: 16
    Last Post: 11-11-2008, 11:02 PM
  4. Replies: 1
    Last Post: 03-17-2005, 07:53 AM
  5. card shuffling array(help!!!!!!!!!)
    By TheWaRpedOnE in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2001, 01:25 PM

Tags for this Thread


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