Chaps
Chaps

Reputation: 57

random generation of fixed range of numbers in array with no duplcate rows in that 2d array

Hi i am working on the problem of generating random numbers with no duplicate rows in the 2d array.

I approached in this way i used random shuffle to do that and code is

    #include <stdlib.h>
    #include <time.h>
    #include<iostream>

    using namespace std;

    int main ( void )
    {
        srand ( time(NULL) );
        int nsize = 4;
        int seq[nsize];
        int i;

        /* Initialize seq to an ordered range */
        for ( i = 0; i <nsize; i++ )
        {
            seq[i] = i+1;
        }
        cout<<"ele in initial array:";
        for(int j=0;j<nsize;j++)
            cout<<seq[j]<<" ";
        cout<<endl;
        int **array = new int *[nsize * 5];
        for(int i=0;i<nsize*5;i++)
            array[i] = new int[nsize];

        /* Random shuffle */
        int k;

        for(k=0;k<20;k++)
        {
            //srand ( time(NULL) );
            for ( i = nsize-1; i >0; i-- ) {
                //srand ( time(NULL) );
                int r = ( rand() % (nsize - 1 ));
                //srand ( time(NULL) );
                int save = seq[i];
                seq[i] = seq[r];
                seq[r] = save;
            }

            for ( i = 0; i < nsize; i++ )
            {
                array[k][i] = seq[i];
            }


            for(int i=0;i<20;i++)
            {
                for(int j=0;j<nsize;j++)
                {
                    cout<<array[i][j]<<" ";
                }
                cout<<endl;
            } 
            return 0;
        }
    }

but the problem is it is generating same rows ex:

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

is there any better way to do that to eliminate the duplicate rows and generate unique rows in 2d array. Thank you all for your help

Upvotes: 0

Views: 235

Answers (3)

SirGuy
SirGuy

Reputation: 10770

This answer is like P0W's except using std::vector instead of arrays and afterward all the permutations are shuffled to randomize their order afterward.

    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <iterator>
    #include <ctime>
    #include <cstdlib>

    int main ()
    {
        std::srand(std::time(NULL));
        std::vector<std::vector<int> > rows;
        unsigned min_value = 1;
        unsigned max_value = 4;
        rows.push_back(std::vector<int>(max_value - min_value + 1));
        for(unsigned i = min_value; i <= max_value; i++)
            rows[0][i - min_value] = i;
        while(std::next_permutation(rows.rbegin()->begin(),
                                    rows.rbegin()->end()))
            rows.push_back(*rows.rbegin());
        rows.push_back(*rows.rbegin());
        std::random_shuffle(rows.begin(), rows.end());

        for(unsigned i = 0; i < rows.size(); i++)
        {
            std::copy(rows[i].begin(), rows[i].end(),
                      std::ostream_iterator<int>(std::cout, " "));
            std::cout << '\n';
        }
    }

Upvotes: 0

P0W
P0W

Reputation: 47784

It looks like you need all possible permutation of 1-4 in 24 rows, you need a way to keep track of your previous row with some temporary matrix

An easy way would be this:-

size_t k=0;
std::sort(arr, arr+nsize);
do {
    for(int i=0;i<nsize;i++)
     array[k][i] = arr[i];
     k++;
} while(std::next_permutation(arr, arr+nsize));

See HERE

Upvotes: 1

Darin Heuermann
Darin Heuermann

Reputation: 1

Track the rows you create. When you create a new row, check that you have not already created that row. If you have, scrap it and create the new row again.

Upvotes: 0

Related Questions