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

1. 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. 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. 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. Then set a[i] to be something other than i, if you don't want 0, 1, 2, 3, 4.

5. I don't follow. Sorry. Like I said, pretty new at this.

6. Do you see where the values of a[i] are being set?

7. 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. Whoops. Not that. Disregard.

9. 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. 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.

11. 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. 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.

13. That seems to work, thanks!

14. 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. Instead of printing to standard output, push_back to a std::vector<int>.