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

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-20-2011
yif611
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 -
• 05-20-2011
tabstop
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.
• 05-20-2011
yif611
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;   }```
• 05-20-2011
tabstop
Then set a[i] to be something other than i, if you don't want 0, 1, 2, 3, 4.
• 05-20-2011
yif611
I don't follow. Sorry. Like I said, pretty new at this.
• 05-20-2011
tabstop
Do you see where the values of a[i] are being set?
• 05-20-2011
yif611
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; }```
?
• 05-20-2011
yif611
Whoops. Not that. Disregard.
• 05-20-2011
yif611
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 -
• 05-20-2011
laserlight
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.
• 05-20-2011
yif611
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;   }```
• 05-20-2011
laserlight
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.
• 05-20-2011
yif611
That seems to work, thanks!
• 05-20-2011
yif611
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?
• 05-21-2011
laserlight
Instead of printing to standard output, push_back to a std::vector<int>.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last