Jump to content


Generating a bunch of random codenumbers

  • Please log in to reply
4 replies to this topic

#1 SharkBait

  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 26 May 2006 - 06:01 PM

Ok, lets see if I can explain this better....

What I am looking to do is figure out a way to create a random code made up of alphanumeric characters.

The reason: So customers can enter this unique, generate code into a website for something special ;)

I would then have to insert the randomly generated codes into a database so that I can check to see if its been used or not used etc.

The code should less than 10 characters.

I am just unsure how I should tackle this. I thought of a hash or sort, but can I make a hash for 10 characters or less?

Perhaps some sort of Algorithm so that when they enter their code I can check to see if it is valid, and then I can check to see if its already used (as in already in the database)

Kind of like how credticard seeds work.

Any thoughts/advice is grealy appreicated :)

#2 .josh

  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 May 2006 - 06:27 PM

umm... you mean like the same thing a random password generator/reset script does, like this:


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 SharkBait

  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 26 May 2006 - 08:00 PM

Yea I guess something like that.

#4 nogray

  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 26 May 2006 - 09:39 PM

I made a small function that will generate random passwords a while back. You can change the $rand string to any characters you want to use.
function createPass(){
    $num_letters = 10;
    $rand = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for($i=0; $i<$num_letters; $i++){
        $pass .= substr($rand, rand(0, strlen($rand)-1), 1);
    return $pass;
hope this helps ya


#5 ryanlwh

  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 26 May 2006 - 10:28 PM

there are a lot of ways to do it. you can hash the current timestamp and then substring the first 10 characters.

also, the above example can be simplified with str_shuffle;
$rand = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$pass = substr(str_shuffle($rand),0,10);

Please use EDIT * 100...
Please use
or [php] * 1000...


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users