C ++ 17 дает нам функцию std::sample
:
#include <iostream>
#include <random>
#include <string>
#include <iterator>
#include <algorithm>
int main()
{
std::string in = "abcdefgh";
std::string out;
std::sample(in.begin(), in.end(),
std::back_inserter(out),
5,
std::mt19937{std::random_device{}()} //(*)
);
std::cout << "five random letters out of " << in << " : " << out << '\n';
}
Если у вас нет C ++ 17, тогда предложение Пример предложения, v4 содержит пример реализации (с использованиемiform_int_distribution):
// the primary function, which calls the detailed functions depending on types of iterators in the used containers.
template< class PopIter, class SampleIter, class Size, class URNG >
SampleIter sample( PopIter first, PopIter last, SampleIter out, Size n, URNG&& g )
{
using pop_t = typename std::iterator_traits< PopIter >::iterator_category;
using samp_t = typename std::iterator_traits< SampleIter >::iterator_category;
return __sample( first, last, pop_t{}, out, samp_t{}, n, forward< URNG >( g ) );
}
template< class PopIter, class SampleIter, class Size, class URNG >
SampleIter __sample( PopIter first,
PopIter last,
std::input_iterator_tag,
SampleIter out,
std::random_access_iterator_tag,
Size n,
URNG&& g )
{
using dist_t = std::uniform_int_distribution< Size >;
using param_t = typename dist_t::param_type;
dist_t d{};
Size sample_sz{0};
while ( first != last && sample_sz != n )
out[ sample_sz++ ] = *first++;
for ( Size pop_sz{sample_sz}; first != last; ++first, ++pop_sz )
{
param_t const p{0, pop_sz};
Size const k{d( g, p )};
if ( k < n )
out[ k ] = *first;
}
return out + sample_sz;
}
template< class PopIter, class SampleIter, class Size, class URNG >
SampleIter __sample( PopIter first,
PopIter last,
std::forward_iterator_tag,
SampleIter out,
std::output_iterator_tag,
Size n,
URNG&& g )
{
using dist_t = std::uniform_int_distribution< Size >;
using param_t = typename dist_t::param_type;
dist_t d{};
Size unsampled_sz = std::distance( first, last );
for ( n = min( n, unsampled_sz ); n != 0; ++first )
{
param_t const p{0, --unsampled_sz};
if ( d( g, p ) < n )
{
*out++ = *first;
--n;
}
}
return out;
}