Results 1 to 3 of 3

Thread: Random Number Qt MacOs

  1. #1
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Arrow Random Number Qt MacOs

    Hello!


    I'm doing a random number in MacOs, but it returns always the same number. i use this:


    #include <ctime> // To seed random generator

    // Randomize
    qsrand( time(0) );

    //to get a random number i use this funtion:

    int myClasse::randInt( int a, int b )
    {
    qsrand( time(0) );
    return a + rand() % ( b - a + 1 );
    }

    but it always return the same number. Why this happens?


    Thanks for read this post...

  2. #2
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Random Number Qt MacOs


    Now i "reset" the qsrand, and for any time i start my application, the random number is not the last one. Thats ok.

    But in the same time the application is running, if i want to get a new Random number several times, it returns always the same number.

    How can i random the random number process to return always different numbers?
    Is it possible?

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Random Number Qt MacOs

    Hi,

    You have to call "srand" only the first time that your application starts. Then every call to "rand" will get you different random values.
    Òscar Llarch i Galán

Similar Threads

  1. Line Number - QTextEdit...???
    By deepusrp in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 16:34
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.