random sub returns same results...

2003-04-05 Thread meriwether lewis
Hi Gurus!

I have the following sub which creates a random 
string. Each time I call the sub in my perl 
script it returns a random string which is fine. 
The problem is every time I run the script the 
same strings are returned. I'm running perl 
5.001 on Windows 2000.

sub RandomStr
{
my $i=0;
my $str=;
foreach(0..9,a..z,A..Z)
{   $i++;
$arr[$i]=$_
}
for($j=0;$jrand(30);$j++)
{
$str.=$arr[rand(58)+1]
}
return $str;
}

So if I run my script calling the above three 
times I get:

brLNh
96
x9k

If I run it again I get the same three strings.
Why?
Is there a better way to get a random string?

Thanks!

Meriwether

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: random sub returns same results...

2003-04-05 Thread Li Ngok Lam
perldoc -f srand

- Original Message - 
From: meriwether lewis [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Saturday, April 05, 2003 10:09 AM
Subject: random sub returns same results...


 Hi Gurus!
 
 I have the following sub which creates a random 
 string. Each time I call the sub in my perl 
 script it returns a random string which is fine. 
 The problem is every time I run the script the 
 same strings are returned. I'm running perl 
 5.001 on Windows 2000.
 
 sub RandomStr
 {
 my $i=0;
 my $str=;
 foreach(0..9,a..z,A..Z)
 {   $i++;
 $arr[$i]=$_
 }
 for($j=0;$jrand(30);$j++)
 {
 $str.=$arr[rand(58)+1]
 }
 return $str;
 }
 
 So if I run my script calling the above three 
 times I get:
 
 brLNh
 96
 x9k
 
 If I run it again I get the same three strings.
 Why?
 Is there a better way to get a random string?
 
 Thanks!
 
 Meriwether
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 
 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: random sub returns same results...

2003-04-05 Thread Rob Dixon
Meriwether Lewis wrote:
 Hi Gurus!

 I have the following sub which creates a random
 string. Each time I call the sub in my perl
 script it returns a random string which is fine.
 The problem is every time I run the script the
 same strings are returned. I'm running perl
 5.001 on Windows 2000.

 sub RandomStr
 {
 my $i=0;
 my $str=;
 foreach(0..9,a..z,A..Z)
 {   $i++;
 $arr[$i]=$_
 }
 for($j=0;$jrand(30);$j++)
 {
 $str.=$arr[rand(58)+1]
 }
 return $str;
 }

 So if I run my script calling the above three
 times I get:

 brLNh
 96
 x9k

 If I run it again I get the same three strings.
 Why?

Perl tries to 'seed' its random number generator from various
system values to make it different each time. Its first attempt
is to read a value from the system device /dev/urandom which
will be used if the read is successful. Clearly this isn't working
properly in your case so perhaps your urandom device is
misbehaving?

An alternative is that you may incorrectly be calling 'srand' to
seed the random number generator. If you pass a constant
to this function then the random number sequence will be
the same each time.

 Is there a better way to get a random string?

If this suits your purpose, then no. You code could be a lot
better laid out though. Take a look at this for inspiration:

sub RandomStr {
my $str;
my @arr = (0..9, a..z, A..Z);
$str .= $arr[rand(@arr)] for 1 .. rand(30);
$str;
}

HTH,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]