Results 1 to 12 of 12

Thread: Qt large random numbers

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt large random numbers

    Does Qt have something for generating large random numbers? Is there a class or function for it (analogous to C rand() but able to handle large numbers). I need to generate random numbers between 1 and 1 million.

  2. #2
    Join Date
    Aug 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt large random numbers

    i haven't tried it yet but i've read that there is qrand and qsrand.

  3. #3
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt large random numbers

    No.
    Qt offers qrand() and a qsrand(...). Both are thread safe as the documentation states. But qrand() is still limited to the macro RAND_MAX (#include <cstdlib>) as the original rand() C++ function.
    RAND_MAX is usually 2^15 -1 = 32767.

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

    timmu (29th August 2009)

  5. #4
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt large random numbers

    Thanks.
    What is the advantage of qrand() orver normal C++ rand()?
    What do people do when they need very large random numbers?
    I'd really appreciate any kind of ideas or links.

  6. #5
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt large random numbers

    As the documentation states, the advantage is that it's thread-safe, so you can call it from different threads at the same time without needing to use mutexes. Other than that, it's the same.

  7. #6
    Join Date
    Jul 2009
    Location
    Italy, Pieve Ligure (GE)
    Posts
    55
    Thanks
    7
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt large random numbers

    Would not rand()*rand() be enough?

    Of course the result would be less random than a single rand(), but maybe random enough for your application.

    Or you may re-init with srand() between the two rand() (or qrand() ) invocations:

    Qt Code:
    1. nRand1 = rand();
    2. srand(<something_suitable>);
    3. nRand = nRand1 * rand();
    To copy to clipboard, switch view to plain text mode 
    then scaling nRand as needed.

    M.

  8. The following user says thank you to miwarre for this useful post:

    timmu (31st August 2009)

  9. #7
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt large random numbers

    rand()*rand() would not be ok because the possibilities of getting low/high numbers are lower than the possibilities of getting middle numbers. Think of dice. If you trow two dice, you have more possibilities of getting 6 than possibilities of getting 12 because to get 12 you need to get 6 twice. But you may get 6 with 1+5, 2+4, 3+3, 4+2 or 5+1. This is a sum but for a multiplication it's even worse. There are numbers you will never get: prime numbers or numbers with more than 2 factors.

    What you can do is:

    Qt Code:
    1. (qrand() * (RAND_MAX+1)) + qrand()
    To copy to clipboard, switch view to plain text mode 

    But you have to keep in mind RAND_MAX may have a different value depending on the platform.
    Last edited by vfernandez; 29th August 2009 at 15:07.

  10. The following user says thank you to vfernandez for this useful post:

    timmu (31st August 2009)

  11. #8
    Join Date
    Jul 2009
    Location
    Italy, Pieve Ligure (GE)
    Posts
    55
    Thanks
    7
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt large random numbers

    Quote Originally Posted by vfernandez View Post
    rand()*rand() would not be ok because...
    Yes, this is what I intended by "less random". But your explanation is much more clear and detailed (agreed on the lack of prime numbers, but in fact numbers with more than 2 factors would be returned, as the numbers returned by each rand() are not necessarily primes).
    Qt Code:
    1. (qrand() * (RAND_MAX+1)) + qrand()
    To copy to clipboard, switch view to plain text mode 
    Or better:
    Qt Code:
    1. nRand1 = qrand();
    2. qsrand(<something_suitable>);
    3. nRand = nRand1 * (RAND_MAX+1) + qrand();
    To copy to clipboard, switch view to plain text mode 
    as the sequence of random numbers is always the same, and without reinitializing srand, after a given result from the first rand() you would always get the same result for the second.

  12. The following user says thank you to miwarre for this useful post:

    timmu (31st August 2009)

  13. #9
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt large random numbers

    agreed on the lack of prime numbers, but in fact numbers with more than 2 factors would be returned, as the numbers returned by each rand() are not necessarily primes
    Oh, that's true. I forgot about that possibility.

  14. The following user says thank you to vfernandez for this useful post:

    timmu (31st August 2009)

  15. #10
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt large random numbers

    you can also make a large number by just combining results of many qrand()s to one number by shifting results and "or-ing" them (just like making one 32-bit integer from four 8-bits integers).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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

    timmu (31st August 2009)

  17. #11
    Join Date
    Jan 2009
    Location
    Midlands UK
    Posts
    62
    Thanks
    6
    Thanked 9 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt large random numbers

    Quote Originally Posted by faldżip View Post
    you can also make a large number by just combining results of many qrand()s to one number by shifting results and "or-ing" them (just like making one 32-bit integer from four 8-bits integers).
    Hi,

    Don't you mean 'xor' the results - if you 'or' a set of numbers the result tends to all 1's

    It is possible to program a psuedo random number generator of any length (bits) by using a number of unsigned words and doing a cascade shift along them and then using a feedback loop from a number of taps along the words (say bits 3, 23, 54 etc), xoring the taps to give a final bit that can be fed back into the first bit of the first word to keep the loop going. There are a number of websites listing such programs to look at, and also there should be some lists of mathematically proven prefered tap points about and prefered seed numbers. I used to use these a lot for 8 and 16 bit micro's.

  18. The following user says thank you to SteveH for this useful post:

    timmu (31st August 2009)

  19. #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: Qt large random numbers

    Go here:
    http://www.nrbook.com/a/bookcpdf.php
    And look at chapter 7.
    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.


Similar Threads

  1. Random Number Qt MacOs
    By Daniela in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2008, 17:40
  2. Sending large datagrams(very large)
    By marcel in forum General Programming
    Replies: 1
    Last Post: 16th February 2008, 21:55
  3. random
    By raphaelf in forum General Programming
    Replies: 9
    Last Post: 6th June 2007, 12:33
  4. Replies: 1
    Last Post: 13th March 2007, 17:33

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.