来自二维网格的不同随机点 [英] Different random points from a 2 dimensional grid

查看:45
本文介绍了来自二维网格的不同随机点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的二维网格,比如 10000 X 10000.从这些网格中我需要选择 1000 个随机点,但我还需要注意两个点都不相同.我想到的标准方法是在选择每个点后,我应该检查所有以前的条目以查看该点是否已被选择,但对于大网格和大量点,这似乎会变得效率低下.有没有更好的方法来做到这一点?我正在使用 C++

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++

推荐答案

随机选择任意点,如果它存在于 Selected Points 列表中,则丢弃它应该不会低效,只要你有选定点的有序集合,您也可以轻松插入.

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.

此外,根据您的点的定义方式(即它们是否与您定义的类或结构相关联),您可以向点对象添加一个布尔变量,名为 Selected.一旦你选择了一个点,检查它是否被标记为Selected.如果没有,请将其添加到您的列表中并将 Selected 值更改为 TRUE.否则,继续选择随机点.

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 as Selected. If not, add it to your list and change the Selected value to TRUE. Otherwise, continue on with your selection of random points.

这篇关于来自二维网格的不同随机点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆