Results 1 to 3 of 3

Thread: Random hex number of given length in QT?

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Random hex number of given length in QT?

    Hi, I wonder if there is a way using plain QT to obtain a random hexadecimal number of any length. I found that it is possible to get 32 characters hex numbers with QUuid::createUuid() (removing {,},-), but what about for others lengths? I'm missing some qt feature or I have to use simple c++ routines?

    Thanks,
    Giuseppe
    Giuseppe CalÃ

  2. #2
    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: Random hex number of given length in QT?

    For simple definitions of "random" you can use qsrand() and then qrand() to get 32, or fewer, bits at a time until you get sufficient bits. If you really want a hexadecimal string then stuff the bytes into a QByteArray and call QByteArray::toHex().

    For any cryptographic purpose you want a better source of randomness and a cryptographic PRNG.


    BTW: A UUID is not entirely random.

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Random hex number of given length in QT?

    Ok, I've followed qsrand()/qrand() way:

    Qt Code:
    1. QTime time = QTime::currentTime();
    2. qsrand((uint)time.msec());
    3.  
    4. ...
    5.  
    6. QString Utils::getRandomHex(const int &length)
    7. {
    8. QString randomHex;
    9.  
    10. for(int i = 0; i < length; i++) {
    11. int n = qrand() % 16;
    12. randomHex.append(QString::number(n,16));
    13. }
    14.  
    15. return randomHex;
    16. }
    To copy to clipboard, switch view to plain text mode 
    Giuseppe CalÃ

  4. The following user says thank you to jiveaxe for this useful post:

    jwgan (31st July 2013)

Similar Threads

  1. random number
    By pingus in forum Qt Programming
    Replies: 3
    Last Post: 6th July 2011, 19:22
  2. How to get a unique random number on mac?
    By punitk in forum Qt Programming
    Replies: 0
    Last Post: 12th October 2010, 17:59
  3. Random number of QGraphicsItem
    By salmanmanekia in forum Newbie
    Replies: 5
    Last Post: 11th June 2010, 12:22
  4. Random Number Qt MacOs
    By Daniela in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2008, 18:40
  5. Replies: 7
    Last Post: 31st May 2006, 10:37

Tags for this Thread

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.