Random numbers in Qt
Hi! I want random number for a few thousands numbers range, but i dont know how to do it.
This is my code and this gives me each program run the same combination of numbers. For example 2,7,5,9... And next time when i run program is the same sequence...
int number=tab.size(); //for now size of my array is 1511 random = qrand() % number;
This is code of the same program but in console and it works good.
It doesnt need to be super perfect random, but although has a different order in each run.
Hi! You forgot to initialize the prng. See: void qsrand(uint seed).
Just add qsrand and you should be good to go.
Your console program works as you want because you're seeding the
randfunction via the
srandcall. The Qt equivalent (thread-safe wrapper) is qsrand. So try something like:
qsrand(time(NULL)); ... random = qrand() % number;
Well... 3 times the same answer :D
@SGaist Must be correct then :)
I'll give a different answer then, use C++11 random. already thread safe and you can generate random numbers according to different probability distribution:
@VRonin you rebel ! ;) :D