Welcome to WebmasterWorld Guest from 50.19.0.90

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

srand - consistent random results

Problem to get consistent random results

     
3:32 pm on Oct 31, 2008 (gmt 0)

Senior Member from DE 

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 20, 2003
posts:870
votes: 4


I'm using a script for many years to get consistent random results (example [webmasterworld.com]) by seeding random number generator with srand($id).

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?

12:24 am on Nov 2, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 4, 2001
posts: 1259
votes: 11


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.

3:38 pm on Nov 2, 2008 (gmt 0)

Senior Member from DE 

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 20, 2003
posts:870
votes: 4


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.

srand($id);
$random = rand(min, max);

has to be the same for the same $id.

That's why it's a "pseudo" random generator.

5:54 pm on Nov 2, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 4, 2001
posts: 1259
votes: 11


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?

6:38 pm on Nov 2, 2008 (gmt 0)

Senior Member from DE 

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 20, 2003
posts:870
votes: 4


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.

8:07 pm on Nov 2, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 4, 2001
posts: 1259
votes: 11


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.

8:26 pm on Nov 2, 2008 (gmt 0)

Senior Member from DE 

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 20, 2003
posts:870
votes: 4


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).

12:35 am on Nov 3, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 4, 2001
posts: 1259
votes: 11


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:

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]

12:46 pm on Nov 3, 2008 (gmt 0)

Senior Member from DE 

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 20, 2003
posts:870
votes: 4


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

6:49 pm on Nov 3, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 4, 2001
posts: 1259
votes: 11


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.

8:38 pm on Nov 3, 2008 (gmt 0)

Senior Member from DE 

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 20, 2003
posts:870
votes: 4


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).

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members