Kushan Peiris
Kushan Peiris

Reputation: 167

shuffling elements in a QStringList

In my Qt c++ application I have a QStringList containing a set of QString values! I want to shuffle(change the positions of the QStrings in the QStringList arbitrarily) . Is there any default function like "shuffle_array" API in perl? If not how can I do it?

eg-

QStringList names;
names<<"John"<<"Smith"<<"Anne";

shuffling may change the positions of John,Smith and Anne arbitrarily! How can I achieve this?

Upvotes: 0

Views: 1302

Answers (1)

kefir500
kefir500

Reputation: 4414

Use the standard std::random_shuffle function:

std::random_shuffle(names.begin(), names.end());    

Also, don't forget to generate a new random number sequence, otherwise the same results will be produced every time:

#include <time.h>

// ...

qsrand(time(NULL));

Upvotes: 2

Related Questions