Page 1 of 1

A question for all you math buffs

Posted: Fri Aug 21, 2009 12:34 pm
by fizix
OK, hopefully there's an easy way to do this but math has never been a strong-point of mine:

I want to generate a random number...sequentially. I know this probably doesn't make any sense so let me try to explain... I have 10 php scripts running as cron jobs. I need them to all generate a random number within a specific range. However, I need to make sure that none of them generate the SAME random number. Would I use a seed to do this?

I tried storing every number I generated in a MySQL database but the database got too big and my host got mad. :banghead:

Re: A question for all you math buffs

Posted: Fri Aug 21, 2009 2:09 pm
by Eran
Use a hashing function (such as MD5, SHA1). Hashes have very (very) low chance of collision, and should be sufficient for your needs.

Also, purging your database periodically as McInfo suggested is probably a good idea.

Re: A question for all you math buffs

Posted: Fri Aug 21, 2009 2:16 pm
by fizix
McInfo wrote:What is the lifetime of a random number? Maybe you could purge the database periodically.
I wish that was a possibility... However, I'm keeping a database of IP addresses and they need to stay in the database forever.
McInfo wrote:Have you tried SQLite?
My web host doesn't support SQLite. :(

Re: A question for all you math buffs

Posted: Fri Aug 21, 2009 2:17 pm
by fizix
pytrin wrote:Use a hashing function (such as MD5, SHA1). Hashes have very (very) low chance of collision, and should be sufficient for your needs.
I'm not exactly sure how I could use a hash in this case. Could you give an example?

Re: A question for all you math buffs

Posted: Fri Aug 21, 2009 2:24 pm
by Eran
Do you simply need a unique identifier or does it have to be a number?

Re: A question for all you math buffs

Posted: Fri Aug 21, 2009 2:32 pm
by fizix
pytrin wrote:Do you simply need a unique identifier or does it have to be a number?
It basically needs to be a number between 1 and 255. I'm actually randomly generating IP addresses and want to make sure I don't generate the same one twice. I guess I need a function I can send a sequential number to and have it output a random number. However, it would need to output the same random number if the same number is passed to it. I think this is basically what a seed does but I'm not sure how to implement it.

Re: A question for all you math buffs

Posted: Fri Aug 21, 2009 2:36 pm
by Eran
output the same random number if the same number is passed to it
This basically means that the output is not random. What you want is a mapping function (ie, a regular math function). In this case hash is an overkill.
What is the range of numbers you have as input?

Re: A question for all you math buffs

Posted: Fri Aug 21, 2009 2:52 pm
by fizix
pytrin wrote:
output the same random number if the same number is passed to it
This basically means that the output is not random. What you want is a mapping function (ie, a regular math function). In this case hash is an overkill.
What is the range of numbers you have as input?
Well, it could be anything... I'd like to start with 1 and increment the number every time the script runs.

Re: A question for all you math buffs

Posted: Fri Aug 21, 2009 3:36 pm
by fizix
OK, I think I found a solution. I set suhosin.srand.ignore = Off in my php.ini for the directory I'm running the script in. Now I can just set srand() to the sequential number and I'll get a different number every time I run the script. I think I can store the sequential number in the database without a problem.