Results 1 to 13 of 13

Thread: Randomization QList<QString> on label

  1. #1
    Join Date
    Mar 2013
    Posts
    45
    Thanks
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Randomization QList<QString> on label

    hi

    i have a QList :


    Qt Code:
    1. QList<QString> str_list ;
    2.  
    3. str_list << "one" << "two" << "three" << "four" << "five" ;
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Randomization QList<QString> on label

    Either:

    Qt Code:
    1. std::shuffle(str_list.begin(), str_list.end());
    2. str_list.takeFirst();
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. QString str = str_list.at(qrand() % str_list.size());
    To copy to clipboard, switch view to plain text mode 

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    smemamian (2nd July 2013)

  4. #3
    Join Date
    Mar 2013
    Posts
    45
    Thanks
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Randomization QList<QString> on label

    i have not std::shuffle .

    ---

    i want an unique random string.
    Last edited by smemamian; 2nd July 2013 at 22:20.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Randomization QList<QString> on label

    Quote Originally Posted by smemamian View Post
    i have not std::shuffle .
    It is part of the standard C++/STL libraries if you have C++11 support:
    Qt Code:
    1. #include <algorithm>
    2. #include <random>
    3. ...
    4. std::random_device rd;
    5. std::mt19937 rng(rd());
    6.  
    7. std::shuffle(list.begin(), list.end(), rng);
    To copy to clipboard, switch view to plain text mode 
    or you can use random_shuffle with or without C++11:
    Qt Code:
    1. #include <algorithm>
    2. ...
    3. std::random_shuffle(str_list.begin(), str_list.end());
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to ChrisW67 for this useful post:

    smemamian (4th July 2013)

  7. #5
    Join Date
    Mar 2013
    Posts
    45
    Thanks
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Randomization QList<QString> on label

    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

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Randomization QList<QString> on label

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    smemamian (4th July 2013)

  10. #7
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Randomization QList<QString> on label

    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?

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Randomization QList<QString> on label

    Please explain what you mean by "didn't work".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #9
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Randomization QList<QString> on label

    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

  13. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Randomization QList<QString> on label

    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.
    Qt Code:
    1. #include <QtDebug>
    2. #include <algorithm>
    3. #include <QList>
    4.  
    5. int main() {
    6. QList<int> list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    7. qDebug() << "Original:" << list;
    8.  
    9. QList<int> s1 = list;
    10. std::random_shuffle(s1.begin(), s1.begin()+5);
    11. qDebug() << "Shuffle 1 (0-4):" << s1;
    12. s1 = list;
    13. std::random_shuffle(s1.begin()+5, s1.begin()+10);
    14. qDebug() << "Shuffle 2 (5-9):" << s1;
    15. s1 = list;
    16. std::random_shuffle(s1.begin()+10, s1.begin()+15);
    17. qDebug() << "Shuffle 3 (10-14):" << s1;
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 

    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)
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #11
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Randomization QList<QString> on label

    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!

  15. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Randomization QList<QString> on label

    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().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:

    Rafaelpsmed (30th October 2014)

  17. #13
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Randomization QList<QString> on label

    i needed both. i worked. i used takeAt because the item cant be chosen more than once. tnks a lot!

Similar Threads

  1. How to display the numbers in a QList<QString> ?
    By harish in forum Qt Programming
    Replies: 4
    Last Post: 3rd January 2012, 04:26
  2. I Want to sort QList<QMap<QString , qreal > >
    By amiref in forum Qt Programming
    Replies: 1
    Last Post: 7th May 2011, 13:33
  3. QList < QHash < int, QString> > ... clear() is slow
    By JoZCaVaLLo in forum Qt Programming
    Replies: 8
    Last Post: 15th March 2011, 11:07
  4. Replies: 7
    Last Post: 16th August 2009, 09:03
  5. Replies: 0
    Last Post: 25th June 2009, 08:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.