[alg.rand.generate]

# 26 Algorithms library [[algorithms]](./#algorithms)

## 26.12 Specialized <random> algorithms [[alg.rand]](alg.rand#generate)

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

[ð](#lib:generate_random)

`template<class R, class G>
  requires [output_range](range.refinements#concept:output_range "25.4.6 Other range refinements [range.refinements]")<R, invoke_result_t<G&>> && [uniform_random_bit_generator](rand.req.urng#concept:uniform_random_bit_generator "29.5.3.3 Uniform random bit generator requirements [rand.req.urng]")<remove_cvref_t<G>>
constexpr borrowed_iterator_t<R> ranges::generate_random(R&& r, G&& g);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L14327)

*Effects*: 

- [(1.1)](#1.1)

  Calls g.generate_random(std​::​forward<R>(r)) if this expression is well-formed[.](#1.1.sentence-1)

- [(1.2)](#1.2)

  Otherwise, if R models [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]"),
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>[.](#1.2.sentence-1)
  [*Note [1](#note-1)*:
  Values of N can differ between invocations[.](#1.2.sentence-2)
 â *end note*]

- [(1.3)](#1.3)

  Otherwise, calls ranges​::​generate(std​::​forward<R>(r), ref(g))[.](#1.3.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L14348)

*Returns*: ranges​::​end(r)[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L14352)

*Remarks*: The effects of generate_random(r, g) shall be equivalent toranges​::​generate(std​::​forward<R>(r), ref(g))[.](#3.sentence-1)

[*Note [2](#note-2)*:

This implies that g.generate_random(a) fills a with the same values as produced by invocation of g()[.](#3.sentence-2)

â *end note*]

[ð](#lib:generate_random_)

`template<class G, [output_iterator](iterator.concept.output#concept:output_iterator "24.3.4.10 Concept output_­iterator [iterator.concept.output]")<invoke_result_t<G&>> O, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<O> S>
  requires [uniform_random_bit_generator](rand.req.urng#concept:uniform_random_bit_generator "29.5.3.3 Uniform random bit generator requirements [rand.req.urng]")<remove_cvref_t<G>>
constexpr O ranges::generate_random(O first, S last, G&& g);
`

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L14370)

*Effects*: Equivalent to:return generate_random(subrange<O, S>(std::move(first), last), g);

[ð](#lib:generate_random__)

`template<class R, class G, class D>
  requires [output_range](range.refinements#concept:output_range "25.4.6 Other range refinements [range.refinements]")<R, invoke_result_t<D&, G&>> && [invocable](concept.invocable#concept:invocable "18.7.2 Concept invocable [concept.invocable]")<D&, G&> &&
           [uniform_random_bit_generator](rand.req.urng#concept:uniform_random_bit_generator "29.5.3.3 Uniform random bit generator requirements [rand.req.urng]")<remove_cvref_t<G>> &&
           is_arithmetic_v<invoke_result_t<D&, G&>>
constexpr borrowed_iterator_t<R> ranges::generate_random(R&& r, G&& g, D&& d);
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L14388)

*Effects*: 

- [(5.1)](#5.1)

  Calls d.generate_random(std​::​forward<R>(r), g) if this expression is well-formed[.](#5.1.sentence-1)

- [(5.2)](#5.2)

  Otherwise, if R models [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]"),
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>[.](#5.2.sentence-1)
  [*Note [3](#note-3)*:
  Values of N can differ between invocations[.](#5.2.sentence-2)
 â *end note*]

- [(5.3)](#5.3)

  Otherwise, callsranges::generate(std::forward<R>(r), [&d, &g] { return invoke(d, g); });

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L14413)

*Returns*: ranges​::​end(r)[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L14417)

*Remarks*: The effects of generate_random(r, g, d) shall be equivalent toranges::generate(std::forward<R>(r), [&d, &g] { return invoke(d, g); })

[*Note [4](#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)[.](#7.sentence-1)

â *end note*]

[ð](#lib:generate_random___)

`template<class G, class D, [output_iterator](iterator.concept.output#concept:output_iterator "24.3.4.10 Concept output_­iterator [iterator.concept.output]")<invoke_result_t<D&, G&>> O, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<O> S>
  requires [invocable](concept.invocable#concept:invocable "18.7.2 Concept invocable [concept.invocable]")<D&, G&> && [uniform_random_bit_generator](rand.req.urng#concept:uniform_random_bit_generator "29.5.3.3 Uniform random bit generator requirements [rand.req.urng]")<remove_cvref_t<G>> &&
           is_arithmetic_v<invoke_result_t<D&, G&>>
constexpr O ranges::generate_random(O first, S last, G&& g, D&& d);
`

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L14439)

*Effects*: Equivalent to:return generate_random(subrange<O, S>(std::move(first), last), g, d);
