Re: Unique random string
| From: | Sascha Schumann | Date: | Wed, 28 Jun 2000 00:56:30 +0000 |
| Subject: | Re: Unique random string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-3485@lists.php.net to get a copy of this message | ||
On Tue, 27 Jun 2000, William Ahern wrote:
> Any ideas on how I can generate a 'guaranteed' unique ascii string less than
> 15 characters.
>
> It must be guaranteed unique across several independent
> servers.
>
> Currently we are generating random short strings to create unique aliases for
> email addresses. The only problem is that to guarantee uniqueness we keep a log
> of previous strings and check to make sure we don't create duplicates (we cannot
> rotate strings. They must be absolutely unqiue for the next decade or so).
>
> The optimal solution would be to generate strings that are truly unique and
> hopefully random enough, w/o having to go through the process of checking for
> duplicates.
>
> I'd use uniqid(), except that it is 32 characters (too long since users will
> have to handle them sometimes) and the algorithm must be available to C, C++,
> Perl, whatever.
Concatenate the current time in milliseconds, attach the
output of lcg_value() (see ext/standard/lcg.c, it is easy to
rip out or emulate in Perl) and calculate the MD5 key of the
string. An MD5 key is 128 bits long, so it fits nicely into
15 chars.
- Sascha