# Rearranging contents of an array

• 11-06-2005
jlf029
Rearranging contents of an array
I have a program to randomly pick 90 questions from a large database(295), so what I did is made an index array of ints of ints labelled 1-90, but I want the array to not have duplicates.

Code:

```        srand(time(0));         for(int i=0;i<MAXQ;i++)                 randoms[i] = rand()%295;```
theres a little snippit.
I then sort the random array, but I get duplicates of an index. How can I arrange 295 questions into 90 but without duplicates?

Thanks
• 11-07-2005
SlyMaelstrom
You could try something like this:

Code:

```int j = 0; for(int i = 0; i < MAXQ; i++) {   randoms[i] = rand() % 295;                   while(j < i) {       if (randoms[i] == randoms[j])         randoms[i] = rand() % 295;       else         j++;   } }```
• 11-07-2005
durban
I'm guessing this int array stores Identifiers for reading the database. You could check to see if one has already come up easily.

Code:

```//!!!! FILL randoms WITH -1!!!! srand(time(0)); int number = 0; bool picked = false; for(int i=0; i<MAXQ; i++) {     number = rand()%295;     for( int j=0; j<i; j++ )     {         if( randoms[j] == number )             picked = true;     }     if( picked == true )         i--; // will go through loop again     else         randoms[i] = number;           picked = false }```
• 11-07-2005
anonytmouse
Code:

```#include <vector> #include <algorithm>  // for random_shuffle using namespace std; int main() {         vector<int> randoms(295);         /* Generate a vector containing the numbers 0..294 */         for (vector<int>::size_type i = 0; i < randoms.size(); i++) randoms[i] = i;         /* Shuffle */         random_shuffle(randoms.begin(), randoms.end());         /* Keep only the first 90 numbers */         randoms.resize(90); }```
• 11-07-2005
jlf029
sweet, thanks guys, all good now.
• 11-07-2005
7stud
random_shuffle(). Nice.

If you didn't want to destroy your vector with the 295 numbers, you could modify the program slightly:
Code:

```#include <iostream> #include <vector> #include <algorithm>  // for random_shuffle using namespace std; int main() {         vector<int> randoms(295);         /* Generate a vector containing the numbers 0..294 */         for (vector<int>::size_type i = 0; i < randoms.size(); i++)                 randoms[i] = i;         /* Shuffle */         random_shuffle(randoms.begin(), randoms.end());         /* Keep only the first 90 numbers */ //        randoms.resize(90);         vector<int> ninety(90);         vector<int>::iterator begin = randoms.begin();         copy(begin, begin + 90, ninety.begin());  //(beginning of range, end of range, destination)                 //output the numbers to the screen:         copy(ninety.begin(), ninety.end(), ostream_iterator<int>(cout, "\n"));                 return 0; }```
• 11-07-2005
hk_mp5kpdw
As a reminder, you should still call srand at the beginning of your program when using the random_shuffle function. The random_shuffle function makes use of rand internally and if you don't call srand you will wind up with the same sequence of "random" values each time the program is run.

Resizing the vector or copying the first 90 elements into another container after the shuffle doesn't hurt but it is unnecessary. You could just simply iterate/loop over the first 90 values and then just stop at that point.[/edit]
• 11-07-2005
Daved
Also, while vectors are definitely preferred, random_shuffle works on C style arrays as well:
Code:

`random_shuffle(randoms, randoms + 295); // assuming randoms has a size of 295`
• 11-07-2005
jlf029
what i came up with the help of u guys...
Code:

```srand(time(0)); //generate random int array         for(int i=0;i<MAXQ;i++)         {                 number = rand()%295;                 for(int j=0;j<i;j++)                 {                         if(randoms[j] == number)                                 picked = true;                 }                 if(picked)                         i--; // will go through loop again                 else                         randoms[i] = number;                 picked = false;         } sorted = new int[ASSIGNED];         for(int i=0;i<ASSIGNED;i++) //get 90 questions from random array                 sorted[i] = randoms[i];         Bubblesort(sorted,ASSIGNED);// sort it```
• 11-07-2005
Daved
That method should work, but it is very inefficient. If you don't want to use the built-in solution of random_shuffle, then you can use the same technique it uses.

Create an index variable and start it at the end (which is MAXQ-1). Get a random number from 0 to that index. Then swap the value at that index with the value at the index of that random number. Decrement the index and repeat the process until the index is 0.

This will effectively shuffle the array without the inefficiency of looking up previously picked numbers. You can then use the first 90 numbers of the array (you can also modify the algorithm to only shuffle 90 numbers, but that is harder so you should start with the basic algorithm).