views:

138

answers:

12

Hello guys,

I have done some searching but really haven't found what I'm looking for. What I would like to do is generate a random BUT unique 5 digit number and push whatever number into an img tag on my page.

For example when people come to my page this number would generate and get pushed into this image tag:

<img src="http://www.sample.com?randomNUM=12345" height="1" width="1" />

I have a mySQL DB and am looking to do this in PHP.

Thanks,

Matt

+3  A: 

What is your purpose for doing this? If you are looking to circumvent caching, you could append the current date/time.

RedFilter
current time and date works for me. I would like to have it formatted like such:06092010130059which it really: 06/09/2010 13:00:59
Matthew
@MAtthew look at `date()`: http://www.php.net/date
Pekka
`time()` is probably an easier and more commonly used approach, unless you absolutely must have a certain date format.
ceejayoz
A: 

I'm sure you are already aware of the relatively low probability of generating a number twice (especially for larger numbers).

However if you insist, you can use a while loop to keep generating random numbers until one that doesn't already exist in the database is found.

Umang
Depends on how many random numbers he's generating. The probability that the 2nd will duplicate the first is only 1/100,000. But the probability that the 50,000 will duplicate one of the preceding ones, assuming they've been unique so far, is 50%. Actually the probability that you'll get at least one duplicate within a fairly small set of picks is surprisingly high. I didn't work it out for 5 digits, but with 2 digits, the probability of a duplicate passes 50% on the 13 try.
Jay
+3  A: 

If you want to circumvent caching, as Red Filter says, better use the current timestamp.

If you really want a unique 5-digit number, you would have to keep track of which numbers you've used in the past. You could use a simple database for this. You create a random number using rand() or, according to the manual, better, mt_rand(), and then query whether it has already been used:

 SELECT FROM mytable WHERE random_number = '$random_number'

repeat until the query returns zero records.

Then, use the number and insert it into a database record:

 INSRT INTO mytable (random_number) VALUES ('$random_number');

If you don't lock the table while you write into it, there is the microscopic possibility of a collision (i.e. two instances of the same script ending up with the same number, and inserting the record, at the same time) but unless you have really massive numbers of requests, I think you can gracefully ignore this.

Pekka
+1  A: 

You can take a look at PHP rand()'s manual.

Babiker
Always choose mt_rand over rand. http://www.php.net/manual/en/function.mt-rand.php
webbiedave
+1  A: 

I'm not an expert on number theory, but I'm not sure you can have a random number and guarantee its uniqueness. See:

https://mywebspace.wisc.edu/lnmaurer/web/rng_stuff/Dilbert0001.jpg

As Red Filter suggests, using the current timestamp is a good approach to prevent caching. Every moment in time is unique.

Erik Mitchell
...with a granularity of 1 second, but that is perhaps sufficient.
Piskvor
+4  A: 

You can use uniqid:

Gets a prefixed unique identifier based on the current time in microseconds.

karim79
This is probably not random.Also, it is not limited to digits.
Turtle
A: 

In fact, as you generate numbers, the randomness of the sequence decreases, since it becomes easier to predict the following. After generated 99999 numbers, the next one won't be random at all.

The timestamp has the tradeoff of possible simultaneous requests that generate the same number (you can decrease this event's probability using get_microtime)

Check out also the uniqid function for a stronger 'uniqueness'.

Iacopo
A: 

Create an array of the 100 000 available 5-digit numbers, shuffle adequately, consume in order.

Ax
A: 

have you looked at the uuid() function in mysql 5.0?

mlm
A: 

Trying to post about this subject but seems like it's not working! Ok let me try again here

Jay
A: 

If you end up not limiting yourself to 5 digits, and are OK with using strings as your key, a fun hack would be to get the time in microseconds [[ http://php.net/manual/en/function.microtime.php ]] and then append a pseudo-random number that is a concatenation of a number of factors based on the user who is making request (like IP address, sessionID, etc), and another random number. Then MD5 the whole thing so that you'll have your result in a common format.

If you can make the assumption that your users will only make one request per microsecond, this should work:

<?php

$Rand = md5($_SERVER['HTTP_CLIENT_IP'].microtime(true).rand(1,999))

?>
Evan
I will check this out and get back to you on this... thanks!
Matthew
A: 

As the number of choices here is pretty manageable, I think a fast implementation would be to create a boolean array with 100,000 entries. As each is used, set the corresponding entry to true. When you need a new one, keep trying until you find an entry that's false.

Jay