从数字向量生成随机数
Random number generation from a vector of numbers
我有一个向量v = {1,5,4,2}
。现在,我想编写一个函数,该函数从该向量返回一对随机数字。示例:{1,2},{4,4},{2,5},{5,1},{1,5},{2,2}
.......我希望以随机的方式生成这对。任何想法如何实施?
auto pair = std::make_pair(v[rand() % v.size()], v[rand() % v.size()]);
是一种方式。
将rand()
从新的<random>
库中切换到C 11的东西,如果您需要发电机具有更好的统计属性:除了发电机本身之外,%
的使用可以引入统计偏置。
这取决于您是否要保证这两个项目是不同的(即,您是否允许您两次绘制同一项目?)
这是两个要求的解决方案和演示:
#include <memory>
#include <vector>
#include <random>
#include <algorithm>
#include <iostream>
#include <cassert>
template<class Rnd, class Iter>
auto random_iterator(Rnd&& rnd, Iter first, Iter last)
{
assert(first != last);
std::size_t size = std::distance(first, last);
auto dist = std::uniform_int_distribution<std::size_t>(0, size - 1);
auto i = dist(rnd);
return std::next(first, i);
}
template<class Iter>
auto remove_iter(Iter last, Iter to_remove)
{
--last;
std::iter_swap(last, to_remove);
return last;
}
// precondition: !vec.empty()
template<class Rnd, class T>
auto select_random_pair(Rnd&& device, std::vector<T> const& vec)
{
auto first = begin(vec), last = end(vec);
// care - we are returning references
auto& a = *random_iterator(device, begin(vec), end(vec));
auto& b = *random_iterator(device, begin(vec), end(vec));
return std::tie(a, b);
}
template<class Iter>
auto make_index_vector(Iter first, Iter last)
{
auto indices = std::vector<std::size_t>(std::distance(first, last));
std::iota(begin(indices), end(indices), std::size_t(0));
return indices;
}
// precondition: vec.size() >= 2
template<class Rnd, class T>
auto select_distinct_random_pair(Rnd&& device, std::vector<T> const& vec)
{
auto indices = make_index_vector(begin(vec), end(vec));
auto first = begin(indices), last = end(indices);
auto a = *(last = remove_iter(last, random_iterator(device, first, last)));
auto b = *random_iterator(device, first, last);
return std::tie(vec[a], vec[b]);
}
int main()
{
auto test_data = std::vector<int> { 1, 2, 3 };
auto rng = std::random_device();
const char* sep = "";
auto emit = [&sep](std::tuple<int const&, int const&> tup)
{
std::cout << sep << "(" << std::get<0>(tup) << ", " << std::get<1>(tup) << ")";
sep = ", ";
};
auto newline = [&sep] { std::cout << 'n'; sep = ""; };
std::cout << "any:n";
for (int i = 0 ; i < 20 ; ++i)
{
emit(select_random_pair(rng, test_data));
}
newline();
std::cout << "distinct:n";
for (int i = 0 ; i < 20 ; ++i)
{
emit(select_distinct_random_pair(rng, test_data));
}
}
示例输出:
any:
(1, 2), (2, 3), (3, 1), (1, 1), (3, 1), (1, 2), (1, 1), (2, 1), (3, 3), (2, 3), (3, 3), (2, 2), (3, 1), (1, 2), (3, 2), (3, 2), (3, 2), (3, 1), (2, 3), (2, 3)
distinct:
(3, 2), (3, 2), (2, 3), (2, 1), (2, 3), (2, 3), (2, 1), (3, 2), (3, 2), (1, 3), (3, 2), (1, 2), (2, 1), (2, 1), (2, 3), (3, 1), (2, 3), (3, 2), (2, 1), (1, 3)
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 整数不会重复超过随机数
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 创建一个带有随机数的向量
- 具有随机数的唯一向量
- 具有从特定范围的随机数初始化特征矩阵或向量初始化特征矩阵或向量的有效方法
- 从数字向量生成随机数
- 非重复随机数 2D 向量 (C++)
- 向量C++中的非重复随机数
- 在向量中插入唯一的随机数
- 用随机数C 填充向量
- 将随机数与向量大小进行比较时出错
- 随机数向量的值在放置在其他对象向量内时设置为 0。
- 用随机数填充向量
- 用不重复的随机数填充向量.为什么不工作
- 用随机数填充多个向量
- 在c++中从向量生成随机数
- 用随机数填充向量