A question for all you math buffs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fizix
Forum Newbie
Posts: 10
Joined: Fri Aug 21, 2009 12:26 pm

A question for all you math buffs

Post 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:
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: A question for all you math buffs

Post 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.
fizix
Forum Newbie
Posts: 10
Joined: Fri Aug 21, 2009 12:26 pm

Re: A question for all you math buffs

Post 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. :(
fizix
Forum Newbie
Posts: 10
Joined: Fri Aug 21, 2009 12:26 pm

Re: A question for all you math buffs

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: A question for all you math buffs

Post by Eran »

Do you simply need a unique identifier or does it have to be a number?
fizix
Forum Newbie
Posts: 10
Joined: Fri Aug 21, 2009 12:26 pm

Re: A question for all you math buffs

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: A question for all you math buffs

Post 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?
fizix
Forum Newbie
Posts: 10
Joined: Fri Aug 21, 2009 12:26 pm

Re: A question for all you math buffs

Post 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.
fizix
Forum Newbie
Posts: 10
Joined: Fri Aug 21, 2009 12:26 pm

Re: A question for all you math buffs

Post 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.
Post Reply