Mar 5, 2001, 7:43 PM
Post #2 of 5
Do you really need a "random" number, or simply a "unique" number?
Re: Creating a random-number based on actual date?
[In reply to]
If you only need a unique tag, and are sure that only one person will ask during any given second on the server you can just do...
A few minutes ago I got a result of '3AA45B45'
my $tag = sprintf("% 08X", time);
The number will continue to increase to '7FFFFFFF' in 2038 or 'FFFFFFFF' in 2106 before you have a problem.
If you need to account for more than one user in a single second you can check on the process id and use a longer tag...
This works under unix, but my windows box is having trouble with the echo. That line is in there so a single script can get several unique tags because the echo being a new process gets a new number each time. But I can get one value on my windows box by using the process id of the script itself as...
$now = time();
$job = `echo \$\$`;
$job = $job % 65536;
$tag = sprintf("% 08X% 04X", $now, $job) ;
NOTE: SPRINTF STATEMENTS HAVE BEEN MANGLED. You will need to remove the space between the percent sign and the number. I had to add them because the page wanted to display the 4 and 8 ascii characters.
$uniq = sprintf("% 08X% 04X", time, $$ % 65536);
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?