Namespaces
Variants
Views
Actions

Talk:cpp/algorithm/random shuffle

From cppreference.com
< Talk:cpp‎ | algorithm
Revision as of 07:13, 30 March 2022 by 217.229.69.239 (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

[edit] Off-by-one error in example?

'last - first' returns the number of elements in the range/container. But the valid index (e.g. for std::vector) available for operator[] runs from 0 ... (last-first-1) for non-empty ranges/containers and the interval for the random number generator is inclusive (contrary to the ususal practise in C++), so it contains both intervall borders. --217.229.69.239 04:56, 30 March 2022 (PDT)

Looks fine to me, max value of i is (last-first-1), so that's the max value that can be returned by std::uniform_int_distribution<diff_t>()(g, param_t(0, i)) --Ybab321 (talk) 05:54, 30 March 2022 (PDT)
A, I see: n is (last-first), but the for()-loop starts with n-1, not with n, for whatever reason. Very confusing. :-( --217.229.69.239 08:13, 30 March 2022 (PDT)