c++ - Generate random coordinates in a rectangle qt -


i have rectangle length 27.5 , width 3.5 , supposed generate coordinates within rectangle unique i.e., without duplicates. number of coordinates need generated based on size of list. here have done far using qt:

struct coordinates_t{     int x;     int y;     };  qvector<coordinates_t> coordinateslist; qlist<qstring> listofdevices; //populate listofdevices  for( int = 0; < listofdevices.count(); i++) { coordinateslist.pushback({rand() % 51 + (-25), rand() % 11 + (-5)}); } 

the problem though rand function generates random numbers within rectangle, not avoid duplicates. there someway in can avoid duplicates , produce unique coordinates within given rectangle.

here example demonstrates check duplicates using qvector , qpoint. if want use qvector::contains own struct, must implement operator==().

#include <qcoreapplication> #include <qvector> #include <qpoint> #include <qdatetime>     #include <qdebug>  int main(int argc, char *argv[]) {     qcoreapplication a(argc, argv);     qvector<qpoint> point_vector;     qsrand(qdatetime::currentmsecssinceepoch());      const int max_points = 20;      while(point_vector.size() < max_points)     {         qpoint point(qrand() % 27, qrand() % 4);         if(!point_vector.contains(point))             point_vector.append(point);     }      qdebug() << point_vector;     return a.exec(); } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -