std::ranges::generate_random
Min standard notice:
Header: <random>
Attempts to generate random numbers with the generate_random member function of the random number generator or the distribution, which is expected to be more efficient. Falls back to element-wise generation if no generate_random member function is available.
# Declarations
Call signature
template< class R, class G >
requires ranges::output_range<R, std::invoke_result_t<G&>> &&
std::uniform_random_bit_generator<std::remove_cvref_t<G>>
constexpr ranges::borrowed_iterator_t<R>
generate_random( R&& r, G&& g );
(since C++26)
template< class G, std::output_iterator<std::invoke_result_t<G&>> O,
std::sentinel_for<O> S >
requires std::uniform_random_bit_generator<std::remove_cvref_t<G>>
constexpr O
generate_random( O first, S last, G&& g );
(since C++26)
template< class R, class G, class D >
requires ranges::output_range<R, std::invoke_result_t<D&, G&>> &&
std::invocable<D&, G&> &&
std::uniform_random_bit_generator<std::remove_cvref_t<G>> &&
std::is_arithmetic_v<std::invoke_result_t<D&, G&>>
constexpr ranges::borrowed_iterator_t<R>
generate_random( R&& r, G&& g, D&& d );
(since C++26)
template< class G, class D, std::output_iterator<std::invoke_result_t<D&, G&>> O,
std::sentinel_for<O> S >
requires std::invocable<D&, G&> &&
std::uniform_random_bit_generator<std::remove_cvref_t<G>> &&
std::is_arithmetic_v<std::invoke_result_t<D&, G&>>
constexpr O
generate_random( O first, S last, G&& g, D&& d );
(since C++26)
# Parameters
first, last: iterator-sentinel pair that denotes the range to which random numbers are writtenr: range to which random numbers are writteng: uniform random bit generatord: random number distribution object
# Notes
At the time of the standardization of std::ranges::generate_random, there is no random number generator or distribution in the standard library that provides a generate_random member function.
std::ranges::generate_random can be more efficient when used with a user-defined random number generator that wraps an underlying vectorized API.
# Example
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <random>
int main()
{
std::default_random_engine eng;
std::default_random_engine::result_type rs[16]{};
std::ranges::generate_random(rs, eng);
std::cout << std::left;
for (int i{}; auto n : rs)
std::cout << std::setw(11) << n << (++i % 4 ? ' ' : '\n');
}