# 27 Algorithms library [algorithms]

## 27.12 Specialized <random> algorithms [alg.rand]

### 27.12.1 General [alg.rand.general]

The contents specified in [alg.rand] are declared in the header <random>.

### 27.12.2generate_random[alg.rand.generate]

```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> ranges::generate_random(R&& r, G&& g); ```
Effects:
• Calls g.generate_random(std​::​forward<R>(r)) if this expression is well-formed.
• Otherwise, if R models sized_range, fills r with ranges​::​size(r) values of type invoke_result_t<G&> by performing an unspecified number of invocations of the form g() or g.generate_random(s), if such an expression is well-formed for a value N and an object s of type span<invoke_result_t<G&>, N>.
[Note 1:
Values of N can differ between invocations.
â€” end note]
• Otherwise, calls ranges​::​generate(std​::​forward<R>(r), ref(g)).
Returns: ranges​::​end(r).
Remarks: The effects of generate_random(r, g) shall be equivalent to ranges​::​generate(std​::​forward<R>(r), ref(g)).
[Note 2:
This implies that g.generate_random(a) fills a with the same values as produced by invocation of g().
â€” end note]
```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 ranges::generate_random(O first, S last, G&& g); ```
Effects: Equivalent to: return generate_random(subrange<O, S>(std::move(first), last), g);
```template<class R, class G, class D> requires output_range<R, invoke_result_t<D&, G&>> && invocable<D&, G&> && uniform_random_bit_generator<remove_cvref_t<G>> constexpr borrowed_iterator_t<R> ranges::generate_random(R&& r, G&& g, D&& d); ```
Effects:
• Calls d.generate_random(std​::​forward<R>(r), g) if this expression is well-formed.
• Otherwise, if R models sized_range, fills r with ranges​::​size(r) values of type invoke_result_t<D&, G&> by performing an unspecified number of invocations of the form invoke(d, g) or d.generate_random(s, g), if such an expression is well-formed for a value N and an object s of type span<invoke_result_t<D&, G&>, N>.
[Note 3:
Values of N can differ between invocations.
â€” end note]
• Otherwise, calls ranges::generate(std::forward<R>(r), [&d, &g] { return invoke(d, g); });
Returns: ranges​::​end(r)
Remarks: The effects of generate_random(r, g, d) shall be equivalent to ranges::generate(std::forward<R>(r), [&d, &g] { return invoke(d, g); })
[Note 4:
This implies that d.generate_random(a, g) fills a with the values with the same random distribution as produced by invocation of invoke(d, g).
â€” end note]
```template<class G, class D, output_iterator<invoke_result_t<D&, G&>> O, sentinel_for<O> S> requires invocable<D&, G&> && uniform_random_bit_generator<remove_cvref_t<G>> constexpr O ranges::generate_random(O first, S last, G&& g, D&& d); ```
Effects: Equivalent to: return generate_random(subrange<O, S>(std::move(first), last), g, d);