Question:
How to populate an array with random unique integers c++?
2013-01-03 03:19:13 UTC
Can someone tell me how to fix this code so that it will populate randarray with unique random integers?

At the moment it generates 5 random numbers (between 1 - 5) but they are not unique

using namespace std;

#include
#include
#define noofmines 5

int gridsize = 5;
int randarray[noofmines];
char grid[5][5];

int main()

{
int n;
srand(time(NULL));
for (n=0; n {

randarray[n]= rand() % noofmines+1;
cout< getchar();
}
}
Five answers:
Yee
2013-01-03 05:51:43 UTC
include

#include

#define noofmines 5

#define num_shuffle 100



int gridsize = 5;



int randarray[noofmines];

char grid[5][5];



int main()

{

int n;

int p1, p2, temp;

srand(time(NULL));

//store 1,2,..5 to array

for (n=0; n
randarray[n] = n+1;

}



//shuffle num_shuffle times

//each time pick 2 index position p1 and p2,

//swap number between these 2 array element

for (n=0; n
{

p1 = rand() % noofmines;

p2 = rand() % noofmines;

temp = randarray[p1];

randarray[p1] = randarray[p2];

randarray[p2] = temp;

}



//print out result

for (n=0; n
cout<
}

getchar();



}
Muhammad Rehan Qadri
2013-01-03 11:33:33 UTC
Remove % noofmines +1.

This is restricting your random number to be from 1-5. How? % of any number (in this case % of random numbers) gives numbers less than that number (in this case numbers generated will be 0 to 4 +1 => 1 to 5) As it's a short range, therefore numbers you are getting are repeated.

After Removing % noofmines +1. You'll get:





#include

#include

#define noofmines 5

using namespace std;

int gridsize = 5;

int randarray[noofmines];

char grid[5][5];



int main()



{

int n;

srand(time(NULL));

for (n=0; n
{



randarray[n]= rand();

cout<
getchar();

}



}
Ratchetr
2013-01-03 13:06:19 UTC
What you really want to do is shuffle the numbers from 1 to noofmines.



You could write your own shuffle code. One of the best algorithms for shuffle is Fisher Yates. Google fisher yates shuffle for more details.



But C++ comes with a built in shuffle routine: std::random_shuffle. It won't work with a regular array, but it will work with a vector. Here is your code modified to use random_shuffle:



#include

#include

#include



#define noofmines 5



int gridsize = 5;

std::vector randarray;

char grid[5][5];



int main()



{

    int n;

    srand(time(NULL));

    for (n=0; n
    {

        randarray.push_back(n+1);

    }



    random_shuffle(randarray.begin(), randarray.end());



    for (n=0; n
    {

        cout<
    }



}



ETA: @yee: Your solution might or might not produce a true random distribution. Didn't try it to find out. But it has the drawback of needing 100 iterations to shuffle 5 numbers. (And presumably the 100 needs to be made bigger if you wanted to shuffle, say 500 numbers). I added a 3'rd link below. It's a good read.
green meklar
2013-01-05 02:47:39 UTC
One thing you can consider: There is a formula that tells you how many possible permutations there are, for a given array length and integer range. If you work backwards, you might be able to apply this formula to turn a random permutation ID back into the permutation associated with it.
2013-01-03 11:42:39 UTC
What you do is whenever you generate new random number just check if it is already there in array or not.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...