...

View Full Version : generate completely random number



Armondo
06-28-2007, 12:16 AM
i am trying to generate numbers that are so random they are not likely to be repeated ever....i currently have this code:

$topicID = mt_rand(0,100);
$topicID .= mt_rand(50,10000);
$topicID .= mt_rand(75,1000000);
$topicID .= mt_rand(100,1000000000000000);
i just thought that that would create a unique number, but seeing that it is a random number...i think that there might be a chance that this $topicID number will be repeated. is there a better way to do this?

Fumigator
06-28-2007, 12:19 AM
If you use a timestamp value out to the millisecond that will never be repeated...

If you're using this value as a unique ID in a MySQL table why not just use an auto-increment integer column?

lpeek
06-28-2007, 12:52 AM
i would go with Fumigator's auto-increment suggestion. the id will never be repeated and automatically increase to the next value.

but if you're certain you need a random number generator let me know... i could help with a function where you say how long you want your number/string and it will return a random alpha-numeric code of your desired length.

Armondo
06-28-2007, 04:02 AM
well, i thought of that...but you can only have one auto increment thing per table...unless its a key. is there a way to do that? i mean, how would i make it to where it does that?

i dont think i can do that though, if it is auto incrementing then each entry gets a new number...i just want the incremented number to be assigned to other posts...but...umm...i really dont know.

Fumigator
06-28-2007, 05:20 AM
You already have an auto-increment ID column in the table? Then why not just use that column?

After inserting a row in a table with an auto-increment key you can grab that value using mysql_insert_id() (http://us2.php.net/manual/en/function.mysql-insert-id.php).

Armondo
06-28-2007, 10:08 PM
ooh, that makes sense...but how do i grab that value before i put it in the database and then duplicate it? like:

$topicID = $id;?

Fumigator
06-28-2007, 10:15 PM
You can't... you can only grab it after the insert is done.


$query = "INSERT INTO topic_table blah blah blah";
$result = mysql_query($query);
if (!$result) {
die("SQL Error in query: $query<br />error text: ".mysql_error());
}
$topicID = mysql_insert_id();

EloyQQQ
06-28-2007, 10:37 PM
Hi.

Take a look at the uniqid() function :)

Armondo
06-29-2007, 07:35 PM
Hi.

Take a look at the uniqid() function :)

thanks alot. i know this is not the best way to accomplish this, but it works. maybe in later versions of my script i will change it...but this is great. i changed the code for the uniqid() to this:

$topicID = md5(uniqid(rand(), true));
i seriously doubt that will be repeated.
output:


d41d8cd98f00b204e9800998ecf8427e


my script is here: http://flashanims.com/forum/index.php

i am trying to code a forum from scratch instead of using prebaked forum software

aedrin
06-29-2007, 10:09 PM
ooh, that makes sense...but how do i grab that value before i put it in the database and then duplicate it? like:

$topicID = $id;?

You want to duplicate a column? Instead of trying to figure out how to do that (a trigger would be possible), why not solve the problem instead of solving the sympton?

If your topic ID is the same as your row ID, then perhaps you should just use the row ID as the topic ID...

There is no reason to generate some fancy number that you only 99% for sure will repeat. An auto increment will never repeat as long as there is no bug in the DBMS.

So 100% or 99%, which is cleaner? ;) Not to mention the auto increment is less code basically.

Armondo
06-30-2007, 12:01 AM
true, but then i would need to make other posts the same row id...and that screws everything up. i think that 99% is good, i seriously doubt that it will repeat. thanks anyway :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum