Title
ranges::generate_random_n is missing
Status
new
Section
[alg.rand.generate]
Submitter
Hewill Kang

Created on 2024-04-29.00:00:00 last changed 1 month ago

Messages

Date: 2024-05-04.14:39:55

Proposed resolution:

This wording is relative to N4981.

  1. Modify [rand.synopsis], header <random> synopsis, as indicated:

    #include <initializer_list>     // see [initializer.list.syn]
    
    namespace std {
      […]
      namespace ranges {
        // [alg.rand.generate], generate_random
        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);
    
        template<class G, output_iterator<invoke_result_t<G&>> O>
          requires uniform_random_bit_generator<remove_cvref_t<G>>
          constexpr O generate_random_n(O first, iter_difference_t<O> n, G&& 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> generate_random(R&& r, G&& g, D&& d);
    
        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 generate_random(O first, S last, G&& g, D&& d);
    
        template<class G, class D, output_iterator<invoke_result_t<D&, G&>> O>
          requires invocable<D&, G&> && uniform_random_bit_generator<remove_cvref_t<G>>
        constexpr O generate_random_n(O first, iter_difference_t<O> n, G&& g, D&& d);
      }
      […]
    }
    
  2. Modify [alg.rand.generate] as indicated:

    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);
    

    -4- Effects: Equivalent to:

    return generate_random(subrange<O, S>(std::move(first), last), g);
    
    
    template<class G, output_iterator<invoke_result_t<G&>> O>
      requires uniform_random_bit_generator<remove_cvref_t<G>>
    constexpr O ranges::generate_random_n(O first, iter_difference_t<O> n, G&& g);
    

    -?- Effects: Equivalent to:

    return generate_random(counted_iterator(std::move(first), n),
                default_sentinel, g).base();
    
    […]
    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);
    

    -8- Effects: Equivalent to:

    return generate_random(subrange<O, S>(std::move(first), last), g, d);
    
    
    template<class G, class D, output_iterator<invoke_result_t<D&, G&>> O>
      requires invocable<D&, G&> && uniform_random_bit_generator<remove_cvref_t<G>>
    constexpr O ranges::generate_random_n(O first, iter_difference_t<O> n, G&& g, D&& d);
    

    -?- Effects: Equivalent to:

    return generate_random(counted_iterator(std::move(first), n),
              default_sentinel, g, d).base();
    
Date: 2024-04-29.00:00:00

P1068 introduced ranges::generate_random which has both range and iterator-sentinel pair overloads. However, the _n version never seems to be discussed.

In view of the fact that generate and fill have their corresponding _n siblings, namely generate_n and fill_n, adding generate_random_n does improve consistency and provide a more intuitive and simple way to generate a specified number of random numbers.

History
Date User Action Args
2024-05-04 14:39:55adminsetmessages: + msg14095
2024-04-29 00:00:00admincreate