Results 1 to 4 of 4

Thread: random number

  1. #1
    Join Date
    Jul 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question random number

    how i create that i get random number betwen 1 and 32?
    now command a = qrand() every time I run the program, it always returns the same numbers.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: random number

    You cannot generate random numbers with a function. Only pseudo-random numbers. qrand returns a deterministic sequence of pseudo-random numbers; the sequence is the same each time because you aren't seeding the sequence differently. You do this with qsrand(uint); you can use a time value for the argument if you want a different result each time.

    This is true of all such functions, unless they are tied to a hardware source of random numbers such as those available on some CPUs. In general, such truly random sources are as necessary as they are rare.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: random number

    It is not possible to generate a perfect randon number sequence just using software, as software behaves always in a predictable pattern.

    You can try time / second / milli second as a randonmize paramter to get better randon sequence.

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

    Default Re: random number

    Put this line In main()
    Qt Code:
    1. qsrand(QDateTime::currentDateTime().toTime_t()
    To copy to clipboard, switch view to plain text mode 
    With this line every time pseudo-random generator will be initialised with another sequence.

Similar Threads

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

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
  •  
Qt is a trademark of The Qt Company.