从数字向量生成随机数

Random number generation from a vector of numbers

本文关键字:随机数 向量 数字      更新时间:2023-10-16

我有一个向量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的东西,如果您需要发电机具有更好的统计属性:除了发电机本身之外,%的使用可以引入统计偏置。

<</p> <</p>

这取决于您是否要保证这两个项目是不同的(即,您是否允许您两次绘制同一项目?)

这是两个要求的解决方案和演示:

#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)