C++ Logo

std-proposals

Advanced search

[std-proposals] `ranges::generate_random_n`?

From: Hewill Kang <hewillk_at_[hidden]>
Date: Mon, 29 Apr 2024 11:30:54 +0800
Hi all,

C++26 introduces P1068
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p1068r11.pdf>:
`ranges::generate_random` to generate random numbers more efficiently. It
currently has the following two overloads (range version and
iterator-sentinel version):

```cpp
template<class R, class G>
  requires output_range<R, invoke_result_t<G&>> &&
  uniform_random_bit_generator<remove_cvref_t<G>>
constexpr borrowed_iterator_t<R> generate_random(R&& r, G&& g);

template<class G, output_iterator<invoke_result_t<G&>> O, sentinel_for<O> S>
  requires uniform_random_bit_generator<remove_cvref_t<G>>
constexpr O generate_random(O first, S last, G&& g);
```
I think it would be valuable to add one `generate_random_n` overload to the
standard since currently both `generate`/`fill` has a corresponding `_n`
version with `generate_n`/`fill_n`.
I don't see any record of this in the document, not sure if this has ever
been discussed, but it does bring consistency IMO.

Is this reasonable? I think this provides a simpler and more intuitive and
simple way to generate a *specific* number of random numbers.
Comments are welcomed.

Hewill

Received on 2024-04-29 03:31:10