Results 1 to 8 of 8

Thread: Two problems with the function qrand()

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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