xso::generator — Shuffling

We have methods to shuffle the elements in a sequence or a container:

template<std::random_access_iterator Iter>
1constexpr void shuffle(Iter b, Iter e);

template<typename Container>
2constexpr void shuffle(Container &c);
1
Shuffles the elements of the iteration \([b,e)\).
No error checking is done, and the behavior is undefined if \(e < b\).
2
Shuffles the elements of container c.
This works on any container that supports std::begin(c) & std::end(c).

Example:

#include <xoshiro.h>
#include "utilities/utilities.h"
int main()
{
    std::array<int, 10> u;
    std::iota(u.begin(), u.end(), 0);
    std::cout << std::format("Original: {}\n", u);
    xso::rng gen;
    gen.shuffle(u);
    std::cout << std::format("Shuffled: {}\n", u);
}

Output: Will vary from run to run

Original: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Shuffled: [4, 5, 7, 9, 0, 8, 2, 1, 3, 6]

See Also

sample

Back to top