As of PHP 5 (and I believe possibly later versions of 4) random numbers are seeded automatically.
You should be able to remove any calls to srand and still get "true" random numbers.
Tanks for the answer. Of course, seeding like srand((double)microtime()*1000000); isn't necessarily since PHP 4.2.0. But it don't need "true" random numbers, I need random numbers which are the same for the same seed, e.g.
$random = rand(min, max);
has to be the same for the same $id.
That's why it's a "pseudo" random generator.
Ah ok, I misunderstood what you were looking for.
The reason they changed this was because predictable random numbers are a security hole.
Wouldn't MD5 (with some kind of pre-obfuscation for security) be a reasonable replacement for what you need?
|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.
Have you tried the OS random number generator? On a linux box it would be '/dev/random', not sure on Windows. I've never needed to use it so I don't know if /dev/random will do what you want but it's worth a look.
|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).
I can't find an official source and I don't remember where I originally read about this.
Did a couple Google searches and found the following related article and post, change seems to have been made in 5.2.6:
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]
Thank you for posting that useful link.
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 second link (which was removed) related directly to the problem you're having.
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.
Thank you for your help.
Finally, I solved the problem by adding
php_value suhosin.srand.ignore 0
to my .htaccess (which changes the local PHP configuration).