• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Create random character strings, prevent re-use, use as few characters as possible

Managing random character strings, preventing re-use, and trying to use as few characters as possible

Hi,

I'm trying to figure out the best method to create a random character string, using typical numbers and letters

0-9 / a-z (non case sensitive)

Goals I am trying to achieve

-will need to query database to see if string is already in use (no biggie if string is found, then repeat until unused string found)
-Can only be up to 5 characters long
-should be as short as possible, no less than 2 characters (if string can only be 2 characters long choose that)

For every string generated, it will be kept in a table..

I need to write a small function to generate these results, so that I can populate the table.

I thought about just doing it in sequence.. 1, 2, 3, 4, etc. using alphabetical characters as well, but I kind of want it to be random.

Any ideas?
 
Why are you trying this instead of just creating a string that has some of time-based value encoded into it? For example, when creating salts for passwords to put into a database I like to take the incoming IP, some time value, and email address concatenate them into a string and then store the SHA1 hash of that. This gives you a predictable length string value, 160-bits, and on top of that you won't have to worry about collisions. With your current scheme, you'll run out of unique values after ~900million records which means you'll have more and more collisions as you get close to that number thus increasing the time to generate new ones. Think of the worst case, say you have 10,000 unique values already stored, worst case you'll have to try and generate 10,001 values before you find a string that is not being used. Eventually the time that it takes you to generate new values will be MUCH longer then time it would take to



 
How many unqiue values do you need? Using 0-9 a-z you only get 36 ^ n disctinct values where n is the number of characters.

Or does part of the homework imply you're going to start with 2-character values then move to longer strings as needed?

"- should be as short as possible, no less than 2 characters (if string can only be 2 characters long choose that) "

Also,
"I thought about just doing it in sequence.. 1, 2, 3, 4, etc. using alphabetical characters as well, but I kind of want it to be random. "

Why? Is that part of the assignment's requirements, or is it extra credit, or just something you feel like doing?
 
Coldfusion.

I'm trying to make a subdomain

company.com/ABCDE

Anytime a url needs to be created off the domain, i want the subdomain part to be randomized.

similar to what Tinyurl.com is doing aka tinyurl.com/A4ZE7

 
Subfolder? A subdomain would be abcd.company.com, like www or mail.

But why the 2-character vs. 5-character length etc.?

And again how many URLs are you expecting? If the number is reasonable you might just pre-generate a table of values, then increment a counter to see which row to use next.
 
It sounds like you want a random number, converted to base 36, which must be between 36^1 and 36^5.

You might want to run a hash function on the URL; then repeat or increment the number until you get something that's new (or is the same as the URL you wanted). You'll probably want to learn all about hash tables in this case.

But why make it as short as possible; why not something longer like Crusty suggests?
 
Originally posted by: Ken g6
It sounds like you want a random number, converted to base 36, which must be between 36^1 and 36^5.

You might want to run a hash function on the URL; then repeat or increment the number until you get something that's new (or is the same as the URL you wanted). You'll probably want to learn all about hash tables in this case.

But why make it as short as possible; why not something longer like Crusty suggests?

Because part of why i'm doing the function is to make a longer url shorter...

The most I'm willing to tolerate is 5 characters after the domain

company.com/12345
 
Originally posted by: TechBoyJK
Originally posted by: Ken g6
It sounds like you want a random number, converted to base 36, which must be between 36^1 and 36^5.

You might want to run a hash function on the URL; then repeat or increment the number until you get something that's new (or is the same as the URL you wanted). You'll probably want to learn all about hash tables in this case.

But why make it as short as possible; why not something longer like Crusty suggests?

Because part of why i'm doing the function is to make a longer url shorter...

The most I'm willing to tolerate is 5 characters after the domain

company.com/12345

make another table and create a record for the part of the url you truncate. concat the truncated url part record id to the end of the url.

 
Originally posted by: brandonbull
Originally posted by: TechBoyJK
Originally posted by: Ken g6
It sounds like you want a random number, converted to base 36, which must be between 36^1 and 36^5.

You might want to run a hash function on the URL; then repeat or increment the number until you get something that's new (or is the same as the URL you wanted). You'll probably want to learn all about hash tables in this case.

But why make it as short as possible; why not something longer like Crusty suggests?

Because part of why i'm doing the function is to make a longer url shorter...

The most I'm willing to tolerate is 5 characters after the domain

company.com/12345

make another table and create a record for the part of the url you truncate. concat the truncated url part record id to the end of the url.

huh?

thats not what I want to do.

i want to generate random characters in a string that is maximum of 5 characters long..

aka use a random() function



 
Use the RandRange function.

You could select a minimum value of 0 and a maximum value of 1048575 and convert the result to a Hex string ((0, 1048575) is equivalent to (00000, FFFFF). That will give you a random, 5-character string using 0-9 and A-F.
 
Back
Top