TikaL13
TikaL13

Reputation: 1488

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

Upvotes: 2

Views: 1979

Answers (11)

Jay
Jay

Reputation: 27492

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.

Upvotes: 0

Evan
Evan

Reputation: 3306

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

?>

Upvotes: 0

Iacopo
Iacopo

Reputation: 4292

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

Upvotes: 0

Ax.
Ax.

Reputation: 687

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

Upvotes: 0

mlm
mlm

Reputation: 1

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

Upvotes: 0

Pekka
Pekka

Reputation: 449783

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.

Upvotes: 3

karim79
karim79

Reputation: 342755

You can use uniqid:

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

Upvotes: 4

Erik Mitchell
Erik Mitchell

Reputation: 425

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.

Upvotes: 1

Babiker
Babiker

Reputation: 18808

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

Upvotes: 1

Umang
Umang

Reputation: 5266

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.

Upvotes: 0

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171559

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

Upvotes: 3

Related Questions