Welcome to WebmasterWorld Guest from 54.211.86.24

Forum Moderators: coopster & jatar k

A coding puzzle

   
8:28 pm on Sep 9, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Here's an interesting challenge I've come against. Any ideas how to solve it elegantly?

I need to generate a globally unique "coupon code". Here are the specs:

- it must be globally unique
- it must have the appearance of being random, ie not easy to guess and it mustn't just be 0001, 0002, 0003, etc.
- it's a string of case-insensitive letters and numbers, like "G263782" OR "IIW99D8".
- it should be not too short, but not too long either. If it's too long, it's more likely someone is going to typo trying to punch it in.
- they mustn't be sequential. We don't want AAAAA to be followed by AAAAB and AAAAC

As for globally unique... you can assume we won't be generating more than one of these per microsecond so something that uses microtime() would be acceptable.

But to be really unique it would be nice to hit the MySQL database and use the Primary key id of the inserted row (autoincrementing integer).

Best answer will receive an awesome little banner made by me to put on their website saying how awesome you are.
8:42 pm on Sep 9, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



this produces something not-too-bad:

strtoupper(base_convert(microtime(true)*100,10,36))

the result is codes that are not strictly sequential, but they act like incrementing numbers. Here are some, taken a few seconds apart:

1OFRH7H9
1OFRH7XF
1OFRH8ED
1OFRH8WY
1OFRH9H0

They don't feel entropic enough to me
8:51 pm on Sep 9, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



strtoupper(base_convert(uniqid(),16,36))

not much better. It still produces what looks like sequential strings.

DKZT9L1S5J
DKZT9QVRYN
DKZT9V0VWD
DKZT9YA4A1
DKZTA2XPBZ
DKZTA7BS5R

Software error:

Can't locate /home/deploy/webmasterworld/code_format-v6.lib in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at decode-post-v6.lib line 27, <THREADDAT> line 5.

For help, please send mail to the webmaster (it@imninjas.com), giving this error message and the time and date of the error.