std::experimental::shuffle(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ NAME πŸŽ€

std::experimental::shuffle β€” std::experimental::shuffle. That’s literally the name, bestie. Iconic behavior, no notes. πŸ’…

okay so this is the std::experimental::shuffle(3) page from the C++ Standard Libary manual β€” yes, β€œLibary,” that’s how it’s spelled in the header, I’m not fixing it 🫢.


✨ Synopsis ✨

Defined in header <experimental/algorithm>, and the vibe is library fundamentals TS v2:

Defined in header <experimental/algorithm>
template< class RandomIt >                      (library fundamentals
TS v2)
void shuffle( RandomIt first, RandomIt last );

The way this works: it reorders the elements in the given range [first, last) so that each possible permutation of those elements has equal probability of appearance, using the per-thread random number engine as the random number generator. That’s the whole slay. 🌸


πŸ’– Parameters πŸ’–

  • first, last β€” the range of elements to shuffle randomly.
  • RandomIt β€” must meet the requirements of ValueSwappable and LegacyRandomAccessIterator.

🫢 Return value 🫢

(none) β€” that’s the whole return value section, bestie. Nothing comes back. πŸ’€


πŸ”₯ Complexity πŸ”₯

Linear in the distance between first and last. Lowkey efficient, highkey iconic. ✨


🌈 Example 🌈

// Run this code β€” yes bestie, run it, live a little πŸ’…

#include <experimental/algorithm>
#include <iostream>
#include <string>

int main()
{
    std::string sample{"ABCDEF"};

    for (int i = 0; i != 4; ++i)
    {
        std::experimental::shuffle(sample.begin(), sample.end());
        std::cout << sample << '\n';
    }
}

Possible output:

DACBFE
CDFBAE
BDCAFE
BAFCED

πŸ‘‘ See also πŸ‘‘

  • random_shuffle β€” randomly re-orders elements in a range; function template; until C++17.
  • shuffle β€” randomly re-orders elements in a range; function template; C++11.

later bestie πŸ’– β€” source: http://cppreference.com; version/date: 2024.06.10. mwah, periodt. πŸŽ€

πŸ“¦ where this page comes from