CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Creating a random-number based on actual date?

 



Chef
Deleted

Mar 5, 2001, 1:57 PM

Post #1 of 5 (485 views)
Creating a random-number based on actual date? Can't Post

Hi,

i need to create a random number with 8 digits based on the actual date and time of the server - additionally the number should start with a number greater than 0 - would be great if anyone could post a code snippet how this will be done.

Thanks.




rGeoffrey
User / Moderator

Mar 5, 2001, 7:43 PM

Post #2 of 5 (481 views)
Re: Creating a random-number based on actual date? [In reply to] Can't Post

Do you really need a "random" number, or simply a "unique" number?

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...


Code
my $tag = sprintf("% 08X", time);

A few minutes ago I got a result of '3AA45B45'

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...


Code
$now = time(); 
$job = `echo \$\$`;
$job = $job % 65536;
$tag = sprintf("% 08X% 04X", $now, $job) ;

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...


Code
$uniq = sprintf("% 08X% 04X", time, $$ % 65536);

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.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


Chef
Deleted

Mar 6, 2001, 1:17 AM

Post #3 of 5 (479 views)
Re: Creating a random-number based on actual date? [In reply to] Can't Post

Hi Geoffrey,

thanks for your hint - is it possible to create a "unique" number (you're right - that's what i need) based only on numbers (no hex value) ?

Regards,
Chef



rGeoffrey
User / Moderator

Mar 6, 2001, 2:15 PM

Post #4 of 5 (476 views)
Re: Creating a random-number based on actual date? [In reply to] Can't Post

Yes, but your unique tag will be longer as you now only have 10 choices for each number rather than 16 hex choices.

Change the sprintf from 'X' for hex numbers to a 'd' and get decimal numbers...


Code
$tag = sprintf("% 10d% 05d", $now, $job) ;

You will want to pad the front or you will have problems with "a very special second in the epoch" that is coming soon. So for now you will not get a file that begins with a 1.




--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


Chef
Deleted

Mar 6, 2001, 2:33 PM

Post #5 of 5 (471 views)
Re: Creating a random-number based on actual date? [In reply to] Can't Post

Geoffrey,

that's it - thanks for your support!

Chef


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives