Qt and random numbers
-
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>
-
-
I know it. I want to generate numbers in some diapason with special seed.
-
I'm not sure what that means (special seed?). Could you elaborate a little bit?
-
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?
-
[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
@ -
Well I thought that solution was implied, my bad.
-
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;
}
@ -
Good solution!
-
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 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