从包含n个元素的向量中随机选择m个元素

编程入门 行业动态 更新时间:2024-10-18 10:31:28
本文介绍了从包含n个元素的向量中随机选择m个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个包含 n 元素的向量。我需要从向量中随机选择 m 元素的子集,而不重复。什么是最有效的方式这样做?我需要在我的代码中做这样几千次。

I have a vector containing n elements. I need to choose a subset of m elements randomly from the vector without repetition. What is the most efficient way of doing this? I need to do this several thousands of times in my code.

我想到的解决方案是使用 rand()生成 0 和 n 之间的随机数 k $ c>。然后选择向量中的 k th元素,并将其插入到 std :: set 中。继续这样做,直到集合的大小等于 m 。我现在放心,该集合包含从 n 元素集合中随机选择的 m 个独特元素。

The solution on top of my mind is to use rand() to generate a random number k between 0 and n. Then pick the kth element in the vector and insert it into a std::set. Keep doing this till the set's size becomes equal to m. I am now assured that the set contains m unique elements randomly chosen from the set of n elements.

其他可能的解决方案是什么?

What are the other possible solutions?

谢谢。

推荐答案

您想要 Fisher-Yates shuffle (停止之后M次迭代):

You want a Fisher-Yates shuffle (stop after M iterations):

template<class fwditer> bidiiter random_unique(fwditer begin, fwditer end, size_t num_random) { size_t left = std::distance(begin, end); while (num_random--) { fwditer r = begin; std::advance(r, rand()%left); std::swap(*begin, *r); ++begin; --left; } return begin; }

在 ideone/3A3cv 。这显然比 std :: random_shuffle 快,当你只需要几个随机数的集合,并应该是大约相同的速度,即使 N == M 。

Demo at ideone/3A3cv. This is significantly faster than std::random_shuffle when you only need a few random numbers out of the set, and should be just about the same speed even if N==M.

更多推荐

从包含n个元素的向量中随机选择m个元素

本文发布于:2023-11-29 12:39:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646419.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   向量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!