Random numbers in Qt
-
wrote on 26 Jan 2017, 20:26 last edited by A Former User
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.
srand(time(NULL)); random=rand()%tab.size();
It doesnt need to be super perfect random, but although has a different order in each run.
-
wrote on 26 Jan 2017, 20:36 last edited by
Hi! You forgot to initialize the prng. See: void qsrand(uint seed).
-
Hi,
Just add qsrand and you should be good to go.
-
wrote on 26 Jan 2017, 20:42 last edited by
-
Well... 3 times the same answer :D
-
wrote on 26 Jan 2017, 20:45 last edited by
@SGaist Must be correct then :)
-
wrote on 27 Jan 2017, 07:09 last edited by VRonin
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:
http://www.cplusplus.com/reference/random/ -
@VRonin you rebel ! ;) :D
1/8