kee2ka4 Posted September 28, 2008 Share Posted September 28, 2008 Hey peeps, I wanted to create a Random and a Unique Number of 8 digits that will represent each unique advert posted by users. I have an advert_id in the database that is the primary key but I do not want to use that on the site for security reasons. So each advert_id will have a unique 8 digit number which will be stored in the table under column name Ad_num. Could anyone guide me how to do this. The 8 digit number has to be unique and random and no fields in the database can have the same number. Thanks, Zub Quote Link to comment https://forums.phpfreaks.com/topic/126146-how-to-create-an-8-digit-unique-and-random-number-for-each-posting-in-php/ Share on other sites More sharing options...
php.ajax.coder Posted September 28, 2008 Share Posted September 28, 2008 You could use a timestamp with a random number added to the end e.g. <?Php echo time() + rand(0,9); ?> Quote Link to comment https://forums.phpfreaks.com/topic/126146-how-to-create-an-8-digit-unique-and-random-number-for-each-posting-in-php/#findComment-652289 Share on other sites More sharing options...
kee2ka4 Posted September 28, 2008 Author Share Posted September 28, 2008 Thanks for your reply, The only problem with timestamp value is that it gives 10 digits but I guess I can do with that. I think if I also add the advert_id to the value it will be certain that it will give me a unique number. Can the time() function ever give the same timestamp value? Quote Link to comment https://forums.phpfreaks.com/topic/126146-how-to-create-an-8-digit-unique-and-random-number-for-each-posting-in-php/#findComment-652315 Share on other sites More sharing options...
AndyB Posted September 28, 2008 Share Posted September 28, 2008 Can the time() function ever give the same timestamp value? Only if time stands still. Quote Link to comment https://forums.phpfreaks.com/topic/126146-how-to-create-an-8-digit-unique-and-random-number-for-each-posting-in-php/#findComment-652318 Share on other sites More sharing options...
thebadbad Posted September 28, 2008 Share Posted September 28, 2008 Can the time() function ever give the same timestamp value? Only if time stands still. And if run more than once within the same second. Quote Link to comment https://forums.phpfreaks.com/topic/126146-how-to-create-an-8-digit-unique-and-random-number-for-each-posting-in-php/#findComment-652320 Share on other sites More sharing options...
thebadbad Posted September 28, 2008 Share Posted September 28, 2008 Check out uniqid(). Quote Link to comment https://forums.phpfreaks.com/topic/126146-how-to-create-an-8-digit-unique-and-random-number-for-each-posting-in-php/#findComment-652322 Share on other sites More sharing options...
CroNiX Posted September 28, 2008 Share Posted September 28, 2008 And if run more than once within the same second. then use microtime(); Quote Link to comment https://forums.phpfreaks.com/topic/126146-how-to-create-an-8-digit-unique-and-random-number-for-each-posting-in-php/#findComment-652363 Share on other sites More sharing options...
Conjurer Posted September 28, 2008 Share Posted September 28, 2008 A random number is not unique. Roll the dice, each roll is a random number. But you will often get the same number repeating. That is part of randomness. You want to create a unique number. Start with a number and increment it. Quote Link to comment https://forums.phpfreaks.com/topic/126146-how-to-create-an-8-digit-unique-and-random-number-for-each-posting-in-php/#findComment-652479 Share on other sites More sharing options...
CroNiX Posted September 28, 2008 Share Posted September 28, 2008 or, create a random number, check db to see if it exists, if not insert it Quote Link to comment https://forums.phpfreaks.com/topic/126146-how-to-create-an-8-digit-unique-and-random-number-for-each-posting-in-php/#findComment-652484 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.