Results 1 to 3 of 3

Thread: doubt in random generated in qt

  1. #1
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Thanks
    65
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default doubt in random generated in qt

    i getting sequence random number when opening closing and opening the app.My code below:
    int Widget::GetRandomString()
    {
    QString possibleCharacters("0123456789");
    int randomStringLength = 4;
    QString randomString;
    for(int i=0; i<randomStringLength; ++i)
    {
    int index = qrand() % possibleCharacters.length();
    QChar nextChar = possibleCharacters.at(index);
    randomString.append(nextChar);

    }
    QFile newfile(PASSCODEFILEPATH);
    if (!newfile.open(QIODevice::ReadWrite))
    {
    return FAIL;
    }
    QTextStream out(&newfile);
    out << randomString.toInt();
    newfile.close();
    }
    i getting the output:
    3781
    7983
    1267
    9819
    1307
    5578
    ..
    i getting these output only,,i need if i run the app output should like 3756 and close the app again open tha app output should be different from previous output.please QT Expered guide me to get proper result,
    Thanks in advance

  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: doubt in random generated in qt

    You need to set a different seed for the number generator each time you start your application.

    http://en.cppreference.com/w/cpp/numeric/random
    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:

    iswaryasenthilkumar (9th July 2015)

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: doubt in random generated in qt

    Somewhere at the beginning in main() put this line :
    Qt Code:
    1. qsrand(QDateTime::currentDateTime().toTime_t());
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Lesiok for this useful post:

    iswaryasenthilkumar (9th July 2015)

Similar Threads

  1. random number
    By pingus in forum Qt Programming
    Replies: 3
    Last Post: 6th July 2011, 18:22
  2. Possible to get random value in QMultiHash/Map?
    By weevil in forum Qt Programming
    Replies: 2
    Last Post: 29th June 2010, 11:00
  3. Replies: 1
    Last Post: 7th April 2010, 16:26
  4. random
    By raphaelf in forum General Programming
    Replies: 9
    Last Post: 6th June 2007, 12:33
  5. opimize random
    By raphaelf in forum Newbie
    Replies: 2
    Last Post: 16th May 2007, 00:01

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.