On Mon, 9 Jul 2001, Purcell, Scott wrote:

> Sorry to be confused, but I believe I did not create a global variable. In
> the code below that I showed, I used a my to localize the $tmp variable
> inside the for loop.?
>
> Anyway, I changed the simple code and added the use strict and use vars
> qw($rand), but now I get no random activity at all. Each time a user hits
> it, they of course receive the same 4 numbers. Not very random ....
>
> I am sure I am screwing this up somehow, was anyone able to get this to
> work, or able to get 4 random numbers each time they run the snippet of
> code?


Scott,

You should start with a simple test script, like this:

#!/usr/bin/perl -wT
  my $r = shift;
  $r->send_http_header("text/plain");
  $r->print("$$ rand: ".rand()."\n");

which does the right thing and then work on extending it to your case,
step by step, checking after every extra code addition (which affects the
rand()). Of course the testing should happen under httpd -X.

Another reason for this weird behavior might be explicit srand() run in
the parent (via startup.pl), and then all children will always start
rand() from the same IV, since PRG will be initialized to the same value.
You cannot detect this with -X mode, on the opposite -- you have to run
this in the normal multi-server mode.

The above example works for me. if I add: srand(time); in startup.pl it
does exactly what you are expreriencing. So I guess that's the real reason
for your problem and not the closure effect.

>
> Thanks
> Scott
>
> -----Original Message-----
> From: Gunther Birznieks [mailto:[EMAIL PROTECTED]]
> Sent: Friday, July 06, 2001 6:22 PM
> To: Purcell, Scott; '[EMAIL PROTECTED]'
> Subject: Re: rand bug?
>
>
> Your problem is likely that the use of a global my is creating a closure
> (see mod_perl guide). Replace it with use vars qw($rand); and don't declare
> your globals using my.
>
> At 01:07 PM 7/6/2001 -0500, Purcell, Scott wrote:
> >Hello,
> >
> >rand bug with mod_perl?
> >"my $rand = int(rand(1000)) always produces '2'" under mod_perl.
> >I have noticed that if I ask for the line above, it always puts $rand at a
> >'2'? So I did some test below, looping through and the first time through
> >with mod_perl, the number is always 2?
> >
> >
> >Under investigation when I run the following code under cgi-bin, it works
> >good like it should.
> >960 is tmp
> >240 is tmp
> >193 is tmp
> >197 is tmp
> >
> >
> >When I run that same code under mod_perl, it looks like this:
> >2 is tmp
> >564 is tmp
> >194 is tmp
> >809 is tmp
> >
> >So basically if I just need one random number I have to do some excess
> work?
> >
> >Does anyone know why this is?
> >If anyone wants just drop the following line under cgi-bin and mod_perl and
> >you'll see the results.
> >
> >Please let me know,
> >Scott
> >
> >
> >
> >#! perl
> >use CGI qw/:standard/;
> >my $q = CGI->new;
> >print $q->header();
> >print $q->start_html("hello");
> >foreach (my $i=1; $i<5; $i++) {
> >     my $tmp = int(rand(1000)+1);
> >     print "$tmp is tmp<br>\n";
> >}
> >
> >
> >Scott Purcell
>
> __________________________________________________
> Gunther Birznieks ([EMAIL PROTECTED])
> eXtropia - The Open Web Technology Company
> http://www.eXtropia.com/
>



_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide
mailto:[EMAIL PROTECTED]   http://apachetoday.com http://eXtropia.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/


Reply via email to