I have a large 2 dimensional grid, let us say 10000 X 10000. From these grid I need to select 1000 random points but I also need to take care that none of the two points are the same. The standard way that comes to my mind is after selecting every point I should check all the previous entries to see if that point has already been selected or not but it seems for large grids and large number of points this will become inefficient. Is there a better way to do it? I am using C++
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
You could implement an algorithm like this:
Randomly selecting any point and then discarding it if it exists in the Selected Points list should not be inefficient, so long as you have well sorted collection of Selected Points, that you can also easily insert into.
Also, depending on how your points are defined (i.e. are they each associated with a class or struct that you've defined), you could add a boolean variable to the point object, named
Selected
. Once you select a point, check to see if it has been marked asSelected
. If not, add it to your list and change theSelected
value toTRUE
. Otherwise, continue on with your selection of random points.Not necessarily. There are two potential sources of inefficiency:
O(1)
time. For this,std::unordered_set
would be a good candidate. The size of the set will grow linearly in the number of elements you need to select, and will be completely independent of the grid size.