mardi 14 juin 2016

Generate random coordinates in a rectangle qt

I have a rectangle with length 27.5 and width 3.5 and I am supposed to generate coordinates within this rectangle which are unique i.e., without any duplicates. The number of coordinates that need to be generated are based on the size of a list. Here is what I have done so far using Qt:

struct coordinates_t{
    int x;
    int y;    
};

QVector<coordinates_t> coordinatesList;
QList<QString> listOfDevices;
//populate listOfDevices

for( int i = 0; i < listOfDevices.count(); i++)
{
coordinatesList.pushback({rand() % 51 + (-25), rand() % 11 + (-5)});
}

The problem now is that even though the rand function generates random numbers within the rectangle, it does not however avoid duplicates. Is there someway in which I can avoid the duplicates and produce unique coordinates within the given rectangle.




Aucun commentaire:

Enregistrer un commentaire