Results 1 to 6 of 6

Thread: Using Qrand in qt

  1. #1
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Smile Using Qrand in qt

    hi,I want to know ,how to use generate value with qrand in qt?

  2. #2
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Using Qrand in qt

    Call
    qsrand(QTime::currentTime().msec())
    once on application start. It will seed the random number generator.

    Then generate the random numbers:
    Qt Code:
    1. int nextNumber = rand() % 15 + 1;
    To copy to clipboard, switch view to plain text mode 

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

    samira (29th June 2013)

  4. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Using Qrand in qt

    did it work fine?

  5. #4
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Using Qrand in qt

    For converting range of
    rand()(0-255)
    into 0-1,is it correct if I use this code?
    Qt Code:
    1. float nextNumber = (rand() % 10000) / 10000.0;
    To copy to clipboard, switch view to plain text mode 


    Added after 4 minutes:


    yes it seems work correct,but i want to sure about it
    Last edited by samira; 29th June 2013 at 13:53.

  6. #5
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Using Qrand in qt

    Yes, this is correct.

  7. #6
    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: Using Qrand in qt

    Quote Originally Posted by samira View Post
    hi,I want to know ,how to use generate value with qrand in qt?
    Assuming you want an integer value in the range 0 to N-1 for N < RAND_MAX then you just do this:
    Qt Code:
    1. int value = qrand() % N;
    To copy to clipboard, switch view to plain text mode 

    However, it seems you wanted a random real value in the range 0 to 1.0, which qrand() does not produce directly:
    Qt Code:
    1. qreal value = static_cast<qreal>(qrand()) / RAND_MAX ;
    To copy to clipboard, switch view to plain text mode 
    Be aware that RAND_MAX is typically fairly small and may give wide gaps between possible "random" floats.

    Neither approach is suitable for cryptographic applications or where the uniformity of the random distribution is very important.

Similar Threads

  1. Some issues with qrand.
    By def in forum Newbie
    Replies: 5
    Last Post: 26th November 2010, 19:57
  2. Two problems with the function qrand()
    By Trader in forum Qt Programming
    Replies: 7
    Last Post: 5th November 2010, 22: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.