Jump to content

[SOLVED] Generate Unique 10 digit number on insert?


Recommended Posts

Hi guys,

Im trying to find the best way to put a unique ID in a table.

 

I currently have a userid as the primary key and it auto increments however i would like to be able to put in a 10digit "ie: 1234567890 " number as a "siteid" for each new registration.

 

Any ideas?

 

I dont really want to use php to generate it as i could end up with the same id further down the line.

 

There are two ways for a computer to generate a random number with no duplicates -

 

1) Generate and store all possible values/combinations. Randomly retrieve one and delete it from the pool of available values,

2) Generate a random number of the correct length and check if it has already been used, repeat until you get a value that has not already been used. Using a length for your number that has at least 10 x more combinations than the maximum number of values you will ever need will reduce collisions and allow this method to quickly find unused values as you use up more of the available combinations.

i know its a little silly, but i want it to use a random 10 digit number rather than

 

100

101

102

103

104

105...

 

This way, people will find it harder to guess other peoples ID's ( not that its a problem if people do guess. I just want it so its a little harder and they are more spread out acorss many numbers. )

OK looks like it may be a bit too far fetched to do this.

 

What ive done instead to generate something unique is:

Result = 923200906091244567354  ( for example )

 

<?php
// Generate unique random number.
$num0 = (rand(10,100));
$num1 = date("Ymd");
$num2 = (rand(100,1000));
$num3 = time();
$randnum = $num0 . $num1 . $num2 . $num3;
// End generate.
?>

 

Thsi way i have a random number and using the date and time.

hoepfully this will create some very odd random uniquie unguessable number.

There could still be some collision.  You should check against your database, if it's there, create another one, if not, proceed.

 

I understand there *could be some collison, However considering it uses date and time, Plus 2 separate random generated smaller numbers mixed in, I think it should be ok.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.