-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Gary,

Look up the rand and srand functions in the perl man pages/help files. I'll 
answer here, 
anyhow, for the benefit of anyone else trying to do something similar..

For your purposes, creating unique files, I would not rely on random number (at 
least not 
random numbers alone), since there is a chance you can generate the same random 
number in two or more different executions of the script, and if you're only 
making 
temporary files, you may end up with a directory cluttered with thousands of 
randomly 
named files (what if your script errors and exits before deleting the files, 
etc?).

What I would suggest is using a filename pattern, such as <PID>-<YYYYMMDD-
HHMMSS>-<RRRR>, where PID is the scripts PID (available to all perl programs as 
the '$$' 
special variable), the YYYY..MMSS is the date and time stamps (as text), and if 
needed, 
RRRR is a random number to help increase the (already high) likelyhood of 
uniqueness. By 
doing this, you could also periodically call a cleanup script that would scan 
for files with 
PID's no longer being used, dates/times too old, etc, and delete them.

In code, this would look something like this (the localtime code is right out 
of the man 
page/help file ;-)):

sub unique_filename {
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $year += 1900; # Fix the encoded year
    $mon += 1; # Fix the zero-based month

    my $randval = rand(10000); # rand(10000) gives random #'s between 0 and 
9999, 
inclusive.

    return sprintf("%5.5u-%4.4u%2.2u%2.2u-%2.2u%2.2u%2.2u-%4.4u",
        $$,
        $year, $mon, $mday,
        $hour, $min, $sec,
        $randval
      );
}

If you're not familiar with sprintf, look it up in the man pages/help files. In 
summary, the % 
indicates the start of a variable to output, the x.y, where x and y are 
numbers, means to 
output output up to x digits, and pad up to y leading zero digits. The final u 
means to expect 
an unsigned integer value to print (many examples use d instead of u, which in 
this case, 
amounts to the same thing). (If that's confusing, either use it as-is, or look 
up the sprintf and 
examples in the man pages/help files ;-))

Regards,
C.


On 30 Jan 2008 at 15:54, Gary Yang wrote:

> Hi, 
> 
> I need to get a random number whenever the perl script is called. Each
> time the random number I got shouldbe different. I use that number to
> name generated files, i.e. I want theperl script to generate different
> file nameswhenever it is called. Can someone tell me how to get the
> different random number whenever the perl script is called? 
> 
> I greatly appreciate your help. 
> 
> 
> Gary 
> 
> 
> 
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try
> it now. 


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)

iD8DBQFHogZCxHjl0yp1r80RAoZUAJ4r1AUDOjEDVcgI/G4biPrlyiAS5wCfdnL3
ER8Ouck+M/k35OvZUdSO8Ag=
=6C4J
-----END PGP SIGNATURE-----
_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to