English 中文(简体)
二维网格中不同的随机点
原标题:Different random points from a 2 dimensional grid
  • 时间:2012-05-25 12:24:52
  •  标签:
  • c++
  • random
< div class=" s- prose js- post-body" 项prop= "text" >

我有一个大二维的网格,让我们说 10000 X 10000 。 我需要从这些网格中选择 1000 个随机点, 但我也需要注意这两个点中没有一个是相同的。 我脑中的标准方式是在选择每个点后都检查所有先前的条目, 看看是否已经选中了该点, 但对于大网格和大量点来说,这将会变得无效。 是否有更好的方法来做到这一点? 我正在使用 C/ p > < / div > 。

最佳回答

随机选择任何点然后丢弃它, 如果它存在于 < em> 选中点 < / 选择点 > 列表中, 则不应无效, 只要您已经对 < em > 选中点 < / 选择点 > 的收藏进行了精密排序, 并且也可以轻松插入 。

另外,根据如何定义您的点(即它们是否都与您定义的类别或曲线相关联),您可以在点对象上添加一个布尔变量,名为 Sselective 。一旦您选择了一个点,请检查它是否被标记为 Sselective 。如果不是,请将其添加到列表中,并将 Sselective 值修改为 TRUE 。否则,继续选择随机点。

问题回答

这对大网格和大量点数看来是无效率的。

不一定,效率低下有两个潜在原因:

  1. Overhead caused by rejection sampling (that is, having to keep trying until you ve found a not-yet-selected point). Given that you re choosing 0.001% of the points, the chances of randomly selecting the same point twice are very small. Therefore, the cost of re-trying should be negligible.
  2. Overhead of checking whether the randomly chosen point has already been selected. If you store all previously selected points in a suitable data structure, this can be done in 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.

您可以实施这样的算法 :

  1. Create an empty mapping from hashes to points
  2. select random point
  3. calculate hash
  4. if hash in mapping, goto 1
  5. save hash & point
  6. if not enough points yet, goto 1




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签