Home: Perl Programming Help: Beginner:
Problem with rand() function



abuschr
Novice

Aug 20, 2002, 4:02 AM


Views: 2222
Problem with rand() function

I've tried to generate some random numbers, so I wrote the following:

for ($1 = 0;$i < 5;$i ++) {
$Rand_Num = rand 256;
}

But I'm keep getting the same number every time, so I tried to write this:

srand;
for ($1 = 0;$i < 5;$i ++) {
$Rand_Num = rand 256;
}

But it still generate the same numbers...

Suggestions anyone?


davorg
Thaumaturge / Moderator

Aug 20, 2002, 4:13 AM


Views: 2221
Re: [abuschr] Problem with rand() function

"srand" is unnecessary in versions of Perl since 5.004.

How do you know that the same numbers are being generated? You're just overwriting the same variable each time. This works for me:

Code
#!/usr/bin/perl -w 

use strict;

for (1 .. 5) {
my $rand = rand 256;
print "$rand\n";
}

The output is (for example):

Code
73.6224858353389 
164.764373087201
50.5015418627618
97.8912094465795
2.42542937702547

If you want an array of random numbers, you could do something like this:

Code
my @random; 

push @random, rand 256 for 1 .. 5;

print "@random";


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


abuschr
Novice

Aug 20, 2002, 4:28 AM


Views: 2219
Re: [davorg] Problem with rand() function

Of course I do not overwrite the variable, I do other stuff with it in the loop, but I keep getting the same value every time...


davorg
Thaumaturge / Moderator

Aug 20, 2002, 4:36 AM


Views: 2218
Re: [abuschr] Problem with rand() function

Well, please show a small complete program that demonstrates your problem.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


abuschr
Novice

Aug 20, 2002, 4:48 AM


Views: 2217
Re: [davorg] Problem with rand() function

Here's a part of my code:


Code
    

my $d = HTTP::Daemon->new(LocalPort => $PORT) or die;

# Getting the calls from the form.
while (my $c = $d->accept) {
if (my $r = $c->get_request) {

....
....
....

# I do not want to get zero in the random number
while($Rand_Num == 0) {
$Rand_Num = rand 256;
}
$KEY = chr($Rand_Num);
# Encrypting ...
my $KeyStr = $KEY x length($Msg);
$Msg = $Msg ^ $KeyStr;
....
....
....



(This post was edited by abuschr on Aug 20, 2002, 5:12 AM)


davorg
Thaumaturge / Moderator

Aug 20, 2002, 5:34 AM


Views: 2213
Re: [abuschr] Problem with rand() function

That's really very little use as we can't run it to see what's going on.

As I said before, if you post a small, but complete program that demonstrates your problem, then we can investigate further.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


abuschr
Novice

Aug 20, 2002, 5:48 AM


Views: 2209
Re: [davorg] Problem with rand() function

Sorry for the misunderstanding but here is a complete program that (at least for me) generates the same numbers all the time:


Code
  

#!/usr/bin/perl
# --------------------------------------------------------------------------
use strict;
use warnings;

my $Rand_Num = 0;
my $KEY;

for (1 ... 5) {

# Generating a random number that is not zero.
while($Rand_Num == 0) {
$Rand_Num = rand 256;
}

# Getting the char represented by this random number.
$KEY = chr($Rand_Num);
print "The key is: '" . $KEY . "' and the random number is: '" . $Rand_Num . "'.\n";
}



The output is:


Code
  

The key is: 'e' and the random number is: '101.386932987954'.
The key is: 'e' and the random number is: '101.386932987954'.
The key is: 'e' and the random number is: '101.386932987954'.
The key is: 'e' and the random number is: '101.386932987954'.
The key is: 'e' and the random number is: '101.386932987954'.



davorg
Thaumaturge / Moderator

Aug 20, 2002, 6:08 AM


Views: 2209
Re: [abuschr] Problem with rand() function

In this program, the problem is that you only set $Rand_Num to zero outside the main loop.

The first time round the loop, $Rand_Num is zero. Therefore the "$Rand_Num = rand 256" code is called and $Rand_Num is set to a value.

The next (and subsequent) times round the loop, $Rand_Num already has a non-zero value, so the "rand" function doesn't get called, leaving $Rand_Num containing the previous value.

You should probably initialise $Rand_Num to zero at the start of each time round the loop. Personally, I'd declare $Rand_Num within the loop.


Code
#!/usr/bin/perl  
# --------------------------------------------------------------------------
use strict;
use warnings;

my $KEY;

for (1 ... 5) {
my $Rand_Num = 0;

# Generating a random number that is not zero.
while($Rand_Num == 0) {
$Rand_Num = rand 256;
}

# Getting the char represented by this random number.
$KEY = chr($Rand_Num);
print "The key is: '" . $KEY . "' and the random number is: '" . $Rand_Num . "'.\n";
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks