Menu ▾ ▴

#1244 Fisher-Yates implementation of shuffle

New
nobody
High
Enhancement
2026-10-28
2026-10-28
skarskors
No

FULL DISCLOSURE: I used ChatGPT.

Changed ShufflePlayState::prepare in "/qmmp-2.4.2/src/qmmpui/playstate.cpp" to the following:

void ShufflePlayState::prepare()
{
    QRandomGenerator *rg = QRandomGenerator::global();

    resetState();

    const int currentIndex = m_model->currentIndex();
    const int trackCount = m_model->trackCount();

    for (int i = 0; i < trackCount; ++i)
    {
        if (i != currentIndex)
            m_shuffled_indexes << i;
    }

    // Fisher-Yates shuffle
    for (int i = m_shuffled_indexes.size() - 1; i > 0; --i)
    {
        const int j = rg->bounded(i + 1);
        m_shuffled_indexes.swapItemsAt(i, j);
    }

    m_shuffled_indexes.prepend(currentIndex);
}

I now get variety in my shuffle and hear songs I forgot I had.
Fisher-Yates calculates all possible outcomes to be equally plausible; QRandomGenerator has a bias so strong I at first thought QMMP used some sort of "DJ"-type algorithm.
Please add the option to utilize a Fisher-Yates shuffle if superseding the base QRandomGenerator is not desired. Thanks.

Discussion


Log in to post a comment.