Forum Moderators: coopster
So far there wasn't any problem with this. Latterly I'm getting problems because even with srand($id) I'm getting different random numbers every time. It seems that the seed doesn't work or that the random number generator is 're-seeded'. Perhaps this was caused by a new PHP version (now it's 5.2.6), but I'm not sure what my provider changed.
How can I ensure getting consistent random number?
Added: I had a look at the configuration and saw that "suhosin.srand.ignore" is set "On". Is this setting causing the problems?
srand($id);
$random = rand(min, max);
has to be the same for the same $id.
That's why it's a "pseudo" random generator.
The reason they changed this was because predictable random numbers are a security hole.
Interesting, I haven't heard about this so far. Is this change automatically implemented in newer PHP versions? If yes, I'll get problems with even more projects (which use similar techniques) running on other servers.
MD5 isn't an option. I most of the cases I need random integers in a range from 1 - 100000. crc32 might work if it's generated almost random values in this range. Otherwise I have to implement my own function to generate pseudo random numbers, but this would be take some time and might be slow.
Yes as I understand it the change is a part of all current and future PHP versions.
Do you have any link where I can look for details? So far I haven't seen anything. Finally this would mean that srand() is useless and things like this [php.net] wouldn't work anymore.
Have you tried the OS random number generator?
Good idea. This might work for some of my other projects but not for this one because I don't have access to the OS (it's a simple webhosting package).
Did a couple Google searches and found the following related article and post, change seems to have been made in 5.2.6:
http://www.securiteam.com/unixfocus/5FP0220OAE.html [securiteam.com]
It's not that srand becomes useless, it presumably still provides a seed for the random number generator, it's just that there is an additional seed generated internally (or so it seems).
[edited by: dreamcatcher at 8:28 am (utc) on Nov. 3, 2008]
[edit reason] Removed forum link. [/edit]
Indeed, it seems that the change just ensures that the intial seed (which is generated internally) is better than before. And this change shouldn't effect re-seeding rand() using srand().
However, srand() currently doesn't work. Therefore, either
- my problem is caused by another change
- the article is incomplete and they also made a change for srand()
- it's a bug which will be fixed
The remaining link is only related in that it involves random numbers and security and was part of the same PHP version update (which included a variety of security patches).
I'm not sure why information on this isn't more readily available but I'm nearly certain it is an intentional change and not a bug.