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:
Problem with rand() function

 



abuschr
Novice

Aug 20, 2002, 4:02 AM

Post #1 of 8 (1457 views)
Problem with rand() function Can't Post

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

Post #2 of 8 (1456 views)
Re: [abuschr] Problem with rand() function [In reply to] Can't Post

"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

Post #3 of 8 (1454 views)
Re: [davorg] Problem with rand() function [In reply to] Can't Post

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

Post #4 of 8 (1453 views)
Re: [abuschr] Problem with rand() function [In reply to] Can't Post

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

Post #5 of 8 (1452 views)
Re: [davorg] Problem with rand() function [In reply to] Can't Post

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

Post #6 of 8 (1448 views)
Re: [abuschr] Problem with rand() function [In reply to] Can't Post

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

Post #7 of 8 (1444 views)
Re: [davorg] Problem with rand() function [In reply to] Can't Post

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

Post #8 of 8 (1444 views)
Re: [abuschr] Problem with rand() function [In reply to] Can't Post

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

 
 


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

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