--
PLEASE NOTE: comp.infosystems.www.authoring.cgi is a
SELF-MODERATED newsgroup. aa.net and boutell.com are
NOT the originators of the articles and are NOT responsible
for their content.
go to http://www.perl.com/ it is in the FAQs there.
HTH
Bob Trieger
co...@ma.ultranet.com
Barry Kaplan wrote:
:
: how can you generate unique random numbers in perl?
Well, I just happen to have recently written a subroutine for just that
purpose!
One key thing to remember as you go over this is that perl cannot
generate a truly random number. It needs some help from you. If you
ask it to randomize and you do that over a long enough period, it will
begin to repeat itself at some point. Many people therefore interject
the time as a randomization modifier, as I have done here, since the
time continually changes. This should not be done, however, if you're
using this to control secure access or the like, as the time, of course,
is predictable.
Here's the routine. You call it as follows:
$yourRandomNumber = &randomNumber($a,$b,$c) ;
Where $a and $b are upper and lower limits for your random number and $c
equals 'whole' if you want a whole number back or 'integer' else if you
want an integer back.
sub randomNumber
{
my ($lowerLimit,$upperLimit,$type) = @_ ; # import passed variables
my $range = ($upperLimit - $lowerLimit) ; # set range to randomize
across
srand(time()); # use time as the
randomization factor
my $randomFactor = rand($range) ; # generate random number
my $output = ($lowerLimit + $randomFactor) ; # add to lower limit to
reach output
if ( $type = "whole" ) # if whole number wanted,
clip off
{ # decimals
$output =~ /^(\d+)\./o ;
}
return ($output) ; # return output
}
Good luck!
--
Hank LeMieux New Media? NEW MEXICO!
Leading New Media and
the most beautiful place you'll ever live
or snowboard . . .
or climb . . .
or mountain bike . . .
or (your escape here . . .)
President President
MERCHANT INTERACTIVE CORP NM Internet Professionals Assn.
72 dues-paying members
representing 65 organizations!
Santa Fe, New Mexico USA New Mexico, USA
(505) 986-8166 <http://www.nmipa.org>
Please note that if your routine is used in a CGI script, and two different
requests come in during the same second, they will both get the same
number assigned (because you use 'srand(time())' to 'seed' rand).
I posted a possible solution here (for CGI scripts) a week or so ago.
:One key thing to remember as you go over this is that perl cannot
:generate a truly random number. It needs some help from you. If you
:ask it to randomize and you do that over a long enough period, it will
:begin to repeat itself at some point. Many people therefore interject
:the time as a randomization modifier, as I have done here, since the
:time continually changes. This should not be done, however, if you're
:using this to control secure access or the like, as the time, of course,
:is predictable.
Using the time by itself only works if you also can ensure that you won't
have two requests within the span of a single second. This could be caused
by the same person running the script twice really quickly, or two
different people using the script at the exact same moment.
The time continually changes, but on some scales a second is a LONG time :-)
..
:Here's the routine. You call it as follows:
..
: srand(time()); # use time as the randomization factor
: my $randomFactor = rand($range) ; # generate random number