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!
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.
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.
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?
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.
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.