PDA

View Full Version : Randomization QList<QString> on label



smemamian
2nd July 2013, 21:23
hi

i have a QList :



QList<QString> str_list ;

str_list << "one" << "two" << "three" << "four" << "five" ;

now, how can i set a string on a label Randomly With each click a button ?

wysota
2nd July 2013, 22:25
Either:


std::shuffle(str_list.begin(), str_list.end());
str_list.takeFirst();

or


QString str = str_list.at(qrand() % str_list.size());

depending on the effect you want to achieve (the first guarantees the same string is not chosen twice in a row, the other does not).

smemamian
2nd July 2013, 23:10
i have not std::shuffle .

---

i want an unique random string.

ChrisW67
3rd July 2013, 00:09
i have not std::shuffle .
It is part of the standard C++/STL libraries if you have C++11 support:


#include <algorithm>
#include <random>
...
std::random_device rd;
std::mt19937 rng(rd());

std::shuffle(list.begin(), list.end(), rng);

or you can use random_shuffle with or without C++11:


#include <algorithm>
...
std::random_shuffle(str_list.begin(), str_list.end());

smemamian
4th July 2013, 16:51
Is this wrong?


std::random_shuffle(random_list.begin(),random_lis t[10]);

because i get this error message :

error: C2780: 'void std::random_shuffle(_RanIt,_RanIt,_Fn1 &)' : expects 3 arguments - 2 provided

wysota
4th July 2013, 16:54
You should pass an interator, not an element (as the second parameter). If you don't want to pass end() but rather shuffle only first 10 elements, pass random_list.begin()+10 as the second parameter.

Rafaelpsmed
29th October 2014, 18:54
hi. in this exemple is possible to do this:

std::random_shuffle(str_list.begin()+10, str_list.begin()+20);?

i tried this but it didnt work. what do you suggest?

wysota
30th October 2014, 13:28
Please explain what you mean by "didn't work".

Rafaelpsmed
30th October 2014, 13:51
ok. i have a QList with 60 items (01 << 02 << 03 ...). i want an output of 6 items from those 60 items. and i want to make 6 shuffles in this list like this: the 1st shuffle is between the first 10 items; the second shuffle is between the 11th and the 19th items, the 3rd between the 20th and 28th items. the 4th, 5th and 6th shuffles will be of the entire list. the final 6 items will be displayed in a qplaintextedit. so what happen is this

std::random_shuffle(list.begin(), list.begin()+10);
QString shuffle1 = list.takeFirst();
ui->plainTextEdit->setText(shuffle1 + ";");
this works great. only the first ten items of the list are shuffled

but when i go to he second and third shuffle:

std::random_shuffle(list.begin()+11, list.begin()+19);
QString shuffle2 = list.takeFirst();
ui->plainTextEdit->setText(shuffle2 + ";");
what happen is that the first ten item are shuffled again

std::random_shuffle(list.begin()+20, list.begin()+28);
QString shuffle3 = list.takeFirst();
ui->plainTextEdit->setText(shuffle3 + ";");
what happen is that the first ten item are shuffled again


so the final output is 3 different shuffles of the first ten items. the 4th, 5th and 6th shuffles work perfect.

how do i solve this issue? the items cannot be repeated.

i hope i made myself clear.

thanks in advance

wysota
30th October 2014, 15:19
In the second shuffle if you use takeFirst() then you are not interacting with element you just shuffled. Same goes for the third shuffle. And remember that takeFirst() actually takes the element out of your list.

#include <QtDebug>
#include <algorithm>
#include <QList>

int main() {
QList<int> list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
qDebug() << "Original:" << list;

QList<int> s1 = list;
std::random_shuffle(s1.begin(), s1.begin()+5);
qDebug() << "Shuffle 1 (0-4):" << s1;
s1 = list;
std::random_shuffle(s1.begin()+5, s1.begin()+10);
qDebug() << "Shuffle 2 (5-9):" << s1;
s1 = list;
std::random_shuffle(s1.begin()+10, s1.begin()+15);
qDebug() << "Shuffle 3 (10-14):" << s1;
return 0;
}

Output:
Original: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
Shuffle 1 (0-4): (5, 4, 2, 3, 1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
Shuffle 2 (5-9): (1, 2, 3, 4, 5, 6, 8, 10, 7, 9, 11, 12, 13, 14, 15)
Shuffle 3 (10-14): (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 12, 14)

Rafaelpsmed
30th October 2014, 15:59
great! i worked! but i forgot to mention that i need to pick one item from the first shuffle, put it in a string so i can display to the user in a qplaintextedit (the same operation with the other shuffles). how i do this. tnks!

wysota
30th October 2014, 16:21
Do you actually need to shuffle those items or do you just want to pick a random item out of a given range of items? If the latter then simply use qrand() and QList::at().

Rafaelpsmed
30th October 2014, 16:52
i needed both. i worked. i used takeAt because the item cant be chosen more than once. tnks a lot!