Qt World Summit: Submit your Presentation

Qrand() is this just a wrapper for rand()? Does it have the same limitations?

  • I couldn't find a page that talked about qrand() or sqrand() . So I'm asking here if it just wraps the functions and has same limitations?

    For example srand() and rand() will always product the same numbers given the same seed. Also it only has 0 - RAND_MAX ( which is usually around 35k)

    Which makes it a rather poor random number generator without some extra work, like building a larger value out of just the first byte of the int returned by rand. But that's another story.

    Is there any other options in QT? If not I'll just do what I said above and build larger data types out of the values returned.

  • The limitations are the same. The only difference between qrand() and standard rand() is thread safety. You'll have to find some C++ lib elsewhere if you want a better random number generator. Just search the Web, there are actually quite many.

  • Take a look at boost::random, its header only and gives you several options for generating random numbers.

  • You want the generated random numbers to be always different ?
    You can use time function to seed random number generator and then generates the random number

    int d=rand();

    The time is a double edged sword :)

Log in to reply