我有一个向量v = {1,5,4,2}
。现在我想编写一个从该向量返回一对随机数的函数。
示例:{1,2},{4,4},{2,5},{5,1},{1,5},{2,2}
.......我希望以随机方式生成此对。
知道如何实现这个吗?
答案 0 :(得分:1)
这取决于您是否要保证这两个项目是否相同(即您是否允许两次绘制相同的项目?)
以下是两个要求的解决方案和演示:
std::shared_ptr
示例输出:
#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));
}
}