coolphpdude Posted July 11, 2008 Share Posted July 11, 2008 Hi there, Is there a quick way of generating a random 10-digit code? it must be 10 digits long, no shorter, no longer. Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/ Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 rand(1000000000,9999999999) Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587597 Share on other sites More sharing options...
dingus Posted July 11, 2008 Share Posted July 11, 2008 <?php $key_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $licence_length = 10 ; for($i=0;$i<$licence_length;$i++){ $chr = $key_characters[rand(0,strlen($key_characters))]; $number .= $chr ; } ?> thats should do it Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587598 Share on other sites More sharing options...
coolphpdude Posted July 11, 2008 Author Share Posted July 11, 2008 You little star. Cheers bud! Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587601 Share on other sites More sharing options...
discomatt Posted July 11, 2008 Share Posted July 11, 2008 <?php $digits = 10; $rand = ''; for ( $i = 0; $i < $digits; $i++ ) $rand .= mt_rand(0,9); echo $rand; ?> Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587603 Share on other sites More sharing options...
dingus Posted July 11, 2008 Share Posted July 11, 2008 mbeals rand is limited to 32768 Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587606 Share on other sites More sharing options...
coolphpdude Posted July 11, 2008 Author Share Posted July 11, 2008 is there a way to check to see if this random number already exists in the database and if it does then create an alternative random number? Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587612 Share on other sites More sharing options...
Lamez Posted July 11, 2008 Share Posted July 11, 2008 ya pull the data from the database, then check it and if the new number equals one of the other one, run it again. Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587613 Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 mbeals rand is limited to 32768 funny, my system had no problem with it Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587620 Share on other sites More sharing options...
coolphpdude Posted July 11, 2008 Author Share Posted July 11, 2008 both ways work a treat. I've gone with dingus's code because it combines numbers and both upper and lowercase code so gives me more chance of it being unique. I just need to figure out the code now to check database to see if it exists and if it does stick it in a while loop to generate a different code. Thanks for your help guys, much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-587629 Share on other sites More sharing options...
dingus Posted July 12, 2008 Share Posted July 12, 2008 just query the database and get the number of results if that is = its unique add it and move on else generate a new one you could probably use a recersive function call for that Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588263 Share on other sites More sharing options...
Barand Posted July 12, 2008 Share Posted July 12, 2008 mbeals rand is limited to 32768 Note: On some platforms (such as Windows) RAND_MAX is only 32768. If you require a range larger than 32768, specifying min and max will allow you to create a range larger than RAND_MAX, or consider using mt_rand() instead. Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588283 Share on other sites More sharing options...
mbeals Posted July 12, 2008 Share Posted July 12, 2008 Yea I read that... And it seems to say that specifying a randmax (like I did) higher then 32768 would work (like it did) Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588456 Share on other sites More sharing options...
chronister Posted July 12, 2008 Share Posted July 12, 2008 Here is one that uses sha1 and time which will most always be a unique code. $code = substr(sha1(time()),0,10)); Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588459 Share on other sites More sharing options...
rarebit Posted July 12, 2008 Share Posted July 12, 2008 it may be somewhat unique (have a low collision rate), but it depends what its going to be used for. Let's say it's generated when you register, if I know roughly when you registered I could generate the possibilities... And i've just noticed, your only using the first 10 chars of the sha, well then there's no guarantee that it'll be unique!!! Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588462 Share on other sites More sharing options...
Barand Posted July 12, 2008 Share Posted July 12, 2008 Exactly, not what you'd call random <?php $a = array(); for ($i=0; $i<1000; $i++) $a[] = substr(sha1(time()),0,10); $ka = array_count_values($a); echo '<pre>', print_r($ka, true), '</pre>'; ?> 1000 generated and all the same Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588466 Share on other sites More sharing options...
chronister Posted July 12, 2008 Share Posted July 12, 2008 yeah, I noticed that if your generating a bunch at once, it will be the same.... but if your only using it once in a while as I am it is fine. I am not using it for registration though..... I use it to pass a transaction to a gift card company and they pass it back. So if they match, then the transaction is valid if not I kill the transaction. So, not the best solution for all situations, but it works for some. Quote Link to comment https://forums.phpfreaks.com/topic/114272-random-10-digit-code/#findComment-588484 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.