开发者

How to generate a random but unique number and display that number within the source of my image tag

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


You can use uniqid:

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


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.


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


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


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.


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.


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


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


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


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

?>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜