1. ## Random Sequences

I need a way to either:

a) Put variables or strings in a random order, i.e. 10 strings containing names of people, output instance could be like this:

George
Bob
Zac
Pat
Peter

and then another isntance:

Bob
Zac
Pat
George
Peter

etc.

or

b) Generate a random sequence of numbers, i.e. generate 10 DIFFERENT random numbers. an instance of output:

1
7
8
9
2
3
6
5
4
10

and then

10
1
8
9
6
4
5
3
2
7

etc. 2. You want a random permutation. Just fill the array with the values you want, then shuffle it randomly:
Code:
```#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int random[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int N = 10;

for ( int i = 0; i < N - 1; i++ ) {
int r = (double)rand() / RAND_MAX * ( N - i );
int save = random[i];
random[i] = random[i + r];
random[i + r] = save;
}
for ( int i = 0; i < N; i++ )
cout<< random[i] <<' ';
cout<<endl;
}``` 3. You don't need us to tell you how to output random numbers. You need to do your own homework. If you're having difficulty with the code you've written, that's one thing, but you can't just post your assignment and expect us to do your work for you. 4. I did write code. It didn't work, and I don't know why. It's so jumbled I didn't want to post it, and it's not an assignment. I'm just doing this because I want to. 5. Originally posted by zowen
I did write code. It didn't work, and I don't know why. It's so jumbled I didn't want to post it, and it's not an assignment. I'm just doing this because I want to.
Thats OK. I'd suggest reworking your own code, and posting it here when it doesn't work. You'll learn a lot that way.  6. ## Re: Random Sequences

So look up srand() and rand() to get it done. 7. A handy way to deal with things of this nature is with the next_permutation function defined in the <algorithm> header. Store your names in a container, such as a vector and the next_permutation function can go through the container and continuously switch around the order of the elements in the container until there are no more permutations to be found. The only thing is that to make sure you get all the possible permutations the elements must exist already ordered (least to greatest) in the container before you start running the permutations on them. As an example, this will write all the various permutations of the names to a file called List.Txt, five names seperated by spaces in different order in each line of the file. With 5 names there are 120 total permutations so there will be 120 lines in the file.
Code:
```#include <algorithm>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

int main()
{
vector<string> StrVect;
ofstream Output("List.Txt");

// Add elements to the container

StrVect.push_back("George");
StrVect.push_back("Bob");
StrVect.push_back("Zac");
StrVect.push_back("Pat");
StrVect.push_back("Peter");

// Make sure elements are first sorted

sort( StrVect.begin(), StrVect.end() );

// Write out all permutations to a file

do
{
copy( StrVect.begin(), StrVect.end(), ostream_iterator<string>(Output," ") );
Output << endl;
} while( next_permutation(StrVect.begin(),StrVect.end()) );

return 0;
}```
If you have 10 items then there are 10! or 3628800 possible permutations and you would probably not want all that displayed to the screen or even a file so you would not want to do things exactly as I have done them. You seem to be saying you only want a couple of those possible 3628800 permutations displayed so you would need to either modify this (easily done) or choose another way to go. And of course this could be easily adapted to using a container of int values instead of the string values. 8. Look up random_shuffle(), it's part of the standard library. Popular pages Recent additions 