Files
2025-10-25 03:02:53 +03:00

69 lines
3.3 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[alg.random.shuffle]
# 26 Algorithms library [[algorithms]](./#algorithms)
## 26.7 Mutating sequence operations [[alg.modifying.operations]](alg.modifying.operations#alg.random.shuffle)
### 26.7.13 Shuffle [alg.random.shuffle]
[🔗](#lib:shuffle)
`template<class RandomAccessIterator, class UniformRandomBitGenerator>
void shuffle(RandomAccessIterator first,
RandomAccessIterator last,
UniformRandomBitGenerator&& g);
template<[random_access_iterator](iterator.concept.random.access#concept:random_access_iterator "24.3.4.13Concept random_­access_­iterator[iterator.concept.random.access]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7Concept sentinel_­for[iterator.concept.sentinel]")<I> S, class Gen>
requires [permutable](alg.req.permutable#concept:permutable "24.3.7.6Concept permutable[alg.req.permutable]")<I> &&
[uniform_random_bit_generator](rand.req.urng#concept:uniform_random_bit_generator "29.5.3.3Uniform random bit generator requirements[rand.req.urng]")<remove_reference_t<Gen>>
I ranges::shuffle(I first, S last, Gen&& g);
template<[random_access_range](range.refinements#concept:random_access_range "25.4.6Other range refinements[range.refinements]") R, class Gen>
requires [permutable](alg.req.permutable#concept:permutable "24.3.7.6Concept permutable[alg.req.permutable]")<iterator_t<R>> &&
[uniform_random_bit_generator](rand.req.urng#concept:uniform_random_bit_generator "29.5.3.3Uniform random bit generator requirements[rand.req.urng]")<remove_reference_t<Gen>>
borrowed_iterator_t<R> ranges::shuffle(R&& r, Gen&& g);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8491)
*Preconditions*: For the overload in namespace std:
- [(1.1)](#1.1)
RandomAccessIterator meets
the [*Cpp17ValueSwappable*](swappable.requirements#:Cpp17ValueSwappable "16.4.4.3Swappable requirements[swappable.requirements]") requirements ([[swappable.requirements]](swappable.requirements "16.4.4.3Swappable requirements"))[.](#1.1.sentence-1)
- [(1.2)](#1.2)
The type remove_reference_t<UniformRandomBitGenerator> meets
the uniform random bit generator ([[rand.req.urng]](rand.req.urng "29.5.3.3Uniform random bit generator requirements")) requirements[.](#1.2.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8503)
*Effects*: Permutes the elements in the range [first, last)
such that each possible permutation of those elements
has equal probability of appearance[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8509)
*Returns*: last for the overloads in namespace ranges[.](#3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8513)
*Complexity*: Exactly (last - first) - 1 swaps[.](#4.sentence-1)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8517)
*Remarks*: To the extent that the implementation of this function makes use
of random numbers, the object referenced by g shall serve as
the implementation's source of randomness[.](#5.sentence-1)