Files
cppdraft_translate/cppdraft/alg/rand.md
2025-10-25 03:02:53 +03:00

6.7 KiB
Raw Blame History

[alg.rand]

26 Algorithms library [algorithms]

26.12 Specialized algorithms [alg.rand]

26.12.1 General [alg.rand.general]

1

#

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

26.12.2 generate_random [alg.rand.generate]

🔗

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

1

#

Effects:

  • (1.1)

    Calls g.generate_random(std::forward(r)) if this expression is well-formed.

  • (1.2)

    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]

  • (1.3)

    Otherwise, calls ranges::generate(std::forward(r), ref(g)).

2

#

Returns: ranges::end(r).

3

#

Remarks: The effects of generate_random(r, g) shall be equivalent toranges::generate(std::forward(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](iterator.concept.output#concept:output_iterator "24.3.4.10Concept output_­iterator[iterator.concept.output]")<invoke_result_t<G&>> O, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7Concept sentinel_­for[iterator.concept.sentinel]")<O> S> requires [uniform_random_bit_generator](rand.req.urng#concept:uniform_random_bit_generator "29.5.3.3Uniform random bit generator requirements[rand.req.urng]")<remove_cvref_t<G>> constexpr O ranges::generate_random(O first, S last, G&& g);

4

#

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

🔗

template<class R, class G, class D> requires [output_range](range.refinements#concept:output_range "25.4.6Other range refinements[range.refinements]")<R, invoke_result_t<D&, G&>> && [invocable](concept.invocable#concept:invocable "18.7.2Concept invocable[concept.invocable]")<D&, G&> && [uniform_random_bit_generator](rand.req.urng#concept:uniform_random_bit_generator "29.5.3.3Uniform 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

#

Effects:

  • (5.1)

    Calls d.generate_random(std::forward(r), g) if this expression is well-formed.

  • (5.2)

    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]

  • (5.3)

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

6

#

Returns: ranges::end(r).

7

#

Remarks: The effects of generate_random(r, g, d) shall be equivalent toranges::generate(std::forward(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](iterator.concept.output#concept:output_iterator "24.3.4.10Concept output_­iterator[iterator.concept.output]")<invoke_result_t<D&, G&>> O, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7Concept sentinel_­for[iterator.concept.sentinel]")<O> S> requires [invocable](concept.invocable#concept:invocable "18.7.2Concept invocable[concept.invocable]")<D&, G&> && [uniform_random_bit_generator](rand.req.urng#concept:uniform_random_bit_generator "29.5.3.3Uniform 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

#

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