Results 1 to 8 of 8

Thread: Two problems with the function qrand()

  1. #1
    Join Date
    May 2010
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation Two problems with the function qrand()

    I am having two problems with the function qrand().

    1) in "randInt1", every time I run the program, it always returns the same numbers: 41, 85, 72, 38, 80, 69, 65, 68, 96, 22.

    2) in "randInt2", he often repeats the numbers.

    How can I create random numbers that are not repeated, and are not the same whenever I run the program?

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QTime>
    3. #include <QDebug>
    4.  
    5. int randInt1(int low, int high) { // returns a random integer value between low and high
    6. return qrand() % ((high + 1) - low) + low;
    7. }
    8.  
    9. int randInt2(int low, int high) { // returns a random integer value between low and high
    10. QTime time = QTime::currentTime(); qsrand((uint)time.msec()); // synchronizes with the current time
    11. return qrand() % ((high + 1) - low) + low;
    12. }
    13.  
    14. int main(int argc, char *argv[]) {
    15. QCoreApplication a(argc, argv);
    16.  
    17. for (int i = 0; i < 10; i++) qDebug() << "Rand1: " << randInt1(0, 100);
    18.  
    19. qDebug() << "\n\n";
    20.  
    21. for (int i = 0; i < 10; i++) qDebug() << "Rand2: " << randInt2(0, 100);
    22.  
    23. return a.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 5th November 2010 at 21:56.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Two problems with the function qrand()

    you need a new seed for each new random set.
    use void qsrand ( uint seed ).
    If you have read qrand() doc, you would have known it.

    Also, the 'Qt' tag is for referncing a class in the Qt documentation.
    For posting code please use the code tags - '#' (the larger one, there are two)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Two problems with the function qrand()

    Quote Originally Posted by Trader View Post
    in "randInt2", he often repeats the numbers.
    Define repeat.
    Is this: 10 10 10 10 11 15 21 21 21 21 21
    Or this: 10 11 12 13 10 11 12 13 10 11 12 13
    Or something else?

  4. #4
    Join Date
    May 2010
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Two problems with the function qrand()

    tbscope:

    this: 10 10 10 10 11 15 21 21 21 21 21


    Added after 28 minutes:


    high_flyer: "you need a new seed for each new random set. use void qsrand ( uint seed ). If you have read qrand() doc, you would have known it."

    I'm using qsrand. See the function: randInt2

    I found this in other forums that spoke the same problem.



    "What you see is the effect of pseudo-randomness. You seed it with the time once, and it generates a sequence of numbers. Since you are pulling a series of random numbers very quickly after each other, you are re-seeding the randomizer with the same number until the next millisecond. And while a millisecond seems like a short time, consider the amount of calculations you're doing in that time."



    "If you make the call fast enough the value of QTime::currentTime().msec() will not change, and you're basically re-seeding qsrand with the same seed, causing the next random number generated to be the same as the prior one."



    I think I need something faster than milliseconds, maybe the CPU clock cycles.

    Is there any solution to this problem?
    Last edited by Trader; 5th November 2010 at 13:49.

  5. #5
    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: Two problems with the function qrand()

    Just use qsrand() only one time in main().
    This is meaningless to using qsrand() every time when You are using qrand().

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

    Trader (5th November 2010)

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

    Default Re: Two problems with the function qrand()

    Lesiok is correct; calling qsrand() more than once is not something you need to do.

    As for repeating numbers: that's going to depend partly on what range you give your functions. If you're generating numbers within a narrow range, repeats are going to occur very frequently.

    Although there are many, many different tests for randomness, the simplest one would be to check the frequency of each number; ideally, they should all occur the same number of times over a large run. You can also concoct series metrics by determining the probability that two, three or more numbers would occur after one another, and compare that to the output of your function.

    My guess is that qrand is doing what it's supposed to do. Nearly all of the problems I've run across involving RNGs can be traced to misunderstandings on the part of those using the routines.

    Note, too, that ALL numerical RNGs repeat the same sequence of numbers over and over and over again; algorithms are chosen that produce series with extremely long periods, but it is the nature of deterministic calculation that no such procedure can ever generate truly random numbers. It can only produce a stream of numbers that displays attributes very close to what would be expected from a truly random process.

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

    Trader (5th November 2010)

  9. #7
    Join Date
    May 2010
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Wink Re: Two problems with the function qrand()

    Lesiok && SixDegrees

    It worked perfectly.

    Thank you!!!

  10. #8
    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: Two problems with the function qrand()

    If one is using a unix system, a good source for random numbers is /dev/random. It has a limited output rate but it's a good way to get a unique random sequence by initializing the seed of the random number generator with data from /dev/random. This allows to get unique sequences on different machines (which is not the case with seed related to the current time as in the example above).
    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. Replies: 0
    Last Post: 10th March 2010, 08:13
  2. Replies: 22
    Last Post: 8th October 2008, 13:54
  3. Problems passing an array of pointers to objects to a function
    By Valheru in forum General Programming
    Replies: 16
    Last Post: 30th June 2008, 00:11
  4. const function parameter problems
    By stevey in forum General Programming
    Replies: 3
    Last Post: 18th December 2006, 22:22
  5. Problems calling C function in C++/Qt class
    By Rayven in forum General Programming
    Replies: 2
    Last Post: 2nd June 2006, 21:32

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.