Qt and random numbers
-
wrote on 10 Nov 2011, 11:43 last edited by
Hi, Qt Developer Network!
My project have to create random "Shultz tables":http://www.st.vstour.info/ShultzTables.html
So it is my first try with qrand() and qsrand().
Can you help me and write howto write a code that will generate random number in diapason 1-25?
Thanks!
<body ></body>
-
wrote on 10 Nov 2011, 11:52 last edited by
-
wrote on 10 Nov 2011, 12:34 last edited by
I know it. I want to generate numbers in some diapason with special seed.
-
wrote on 10 Nov 2011, 12:48 last edited by
I'm not sure what that means (special seed?). Could you elaborate a little bit?
-
wrote on 10 Nov 2011, 13:30 last edited by
Not sure what you need, I guess a random number between 1 and 25. I don't know if there is a way to limit qrand (different from MAX_RAND), but maybe you can iterate over qrand to get a limited value?
-
wrote on 10 Nov 2011, 13:52 last edited by
[quote author="fluca1978" date="1320931836"]... but maybe you can iterate over qrand to get a limited value?[/quote]
In this situation the best method, in my opinion is:
@
qrand() % 25 + 1
@ -
wrote on 10 Nov 2011, 14:03 last edited by
Well I thought that solution was implied, my bad.
-
wrote on 10 Nov 2011, 17:21 last edited by
You just need to implement your own nSeeder, try to start from getting current time's second.
@
qsrand(nSeeder);
inline int rangedRand(unsigned int min, unsigned int max){
return (qrand() % (max-min)+1) + min;
}
@ -
wrote on 11 Nov 2011, 07:34 last edited by
Good solution!
-
wrote on 17 Sept 2014, 08:19 last edited by
Sorry for my post into such an old thread, but it must be:
@inline int rangedRand(unsigned int min, unsigned int max){
return (qrand() % (max+1-min)) + min;
}@ -
wrote on 17 Sept 2014, 09:56 last edited by
[quote author="rentner323" date="1410941949"]Sorry for my post into such an old thread, but it must be:
@inline int rangedRand(unsigned int min, unsigned int max){
return (qrand() % (max+1-min)) + min;
}@[/quote]Correct, curse me for the bracket