PDA

View Full Version : Qt large random numbers

timmu
28th August 2009, 16:37
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.

odee2004
28th August 2009, 16:50
i haven't tried it yet but i've read that there is qrand and qsrand.

Boron
28th August 2009, 16:52
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.

timmu
29th August 2009, 08:36
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.

vfernandez
29th August 2009, 08:59
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.

miwarre
29th August 2009, 10:38
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:

nRand1 = rand();
srand(<something_suitable>);
nRand = nRand1 * rand();

then scaling nRand as needed.

M.

vfernandez
29th August 2009, 15:01
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:

(qrand() * (RAND_MAX+1)) + qrand()

But you have to keep in mind RAND_MAX may have a different value depending on the platform.

miwarre
29th August 2009, 17:19
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).

(qrand() * (RAND_MAX+1)) + qrand()
Or better:

nRand1 = qrand();
qsrand(<something_suitable>);
nRand = nRand1 * (RAND_MAX+1) + qrand();
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.

vfernandez
29th August 2009, 21:53
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.

faldzip
30th August 2009, 12:19
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).

SteveH
30th August 2009, 18:56
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.

wysota
31st August 2009, 08:22
Go here:
http://www.nrbook.com/a/bookcpdf.php
And look at chapter 7.