Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

rand with Perl/Tk

by biochris (Beadle)
on Feb 14, 2005 at 20:34 UTC ( [id://430924]=perlquestion: print w/replies, xml ) Need Help??

biochris has asked for the wisdom of the Perl Monks concerning the following question:

How come rand works nice in a console program, but in Tk gives a repeating patern. (For seeding rand I use: srand(time|$$);) I work with ActivePerl on Win32. Thanks a lot for any help.

Replies are listed 'Best First'.
Re: rand with Perl/Tk
by Roy Johnson (Monsignor) on Feb 14, 2005 at 20:49 UTC
    You should not be seeding the random number generator, especially not with a value that will statistically have more 1s than 0s. That doesn't answer your question, it's just something else you should be aware of.
    % perldoc -q rand Found in /home/columbia3/gclib/pkg/perl/lib/5.8.0/pod/perlfaq4.pod Why aren't my random numbers random? If you're using a version of Perl before 5.004, you must call "srand" once at the start of your program to seed the random number generator. 5.004 and later automatically call "srand" at the beginning. Don't call "srand" more than once--you make your numbers less random, rather than more.

    Caution: Contents may have been coded under pressure.
      Excellent point!! Thanks
Re: rand with Perl/Tk
by zentara (Archbishop) on Feb 14, 2005 at 21:14 UTC
    Try this: (just make a bigger number for greater sample base)
    #!/usr/bin/perl use Tk; my $i=1; my $mw=tkinit; my $text = $mw->Scrolled('Text')->pack; $mw->Button(-text=>'Get rand', -command => sub{ for (1..100){ my $rand = rand(10000000); $text->insert('end',"$rand "); } $text->insert('end',"\n\n\n"); } )->pack; MainLoop; __END__

    I'm not really a human, but I play one on earth. flash japh
Re: rand with Perl/Tk
by Errto (Vicar) on Feb 14, 2005 at 20:44 UTC

    I just tried the following on ActivePerl on Win32.

    use Tk; my $mw = Tk::MainWindow->new; my $txt = $mw->Label(-text=>"Value:"); $txt->pack; $mw->Button(-text=>"Ok", -command=>sub {$txt->configure(-text=>"Value: + " . rand)})->pack; srand (time | $$); $mw->MainLoop;
    And I don't see any pattern. Perhaps a bit more detail would help?

    Update: Note that in my example I only call srand once. I tried it again with the srand call inside the callback function and I got a similar result to yours.

Re: rand with Perl/Tk
by renz (Scribe) on Feb 14, 2005 at 21:06 UTC

    As I recall, there hasn't been a need to seed rand since Perl 5.004.. In newer versions, rand is automatically seeded if called.

    PS - And if I was seeding rand, I wouldn't seed it that way... See perldoc -f srand, perldoc -f rand, perldoc -q rand.

    Update: Oh. Well. Maybe if I answered questions in a timely manner, instead of floating on a node for awhile and not refreshing to see if any answers have been added, then I wouldn't have duplicated Roy's post :).

    /renz.
    Put the gun to my head but it was loaded with blanks/
    I was laughing all the way to the bank." --Peter Laughner, Dear Richard.

Re: rand with Perl/Tk
by Anonymous Monk on Feb 15, 2005 at 09:52 UTC
    It shouldn't. However, there's something to say about srand. Since you don't include any code, I can only guess what is going wrong. My guess is that you are doing something like:
    sub button_press { srand(time|$$); my $rand = rand(); ... display $rand ... }
    That's bad. Really bad. You shouldn't be calling srand more than once in your program, and for any Perl produced in the last 6 years or so, there's no need to call srand at all (unless you want a particular sequence). However, calling srand with a decent seed should still produce different numbers - if only because the seed is different. But time|$$ is a really lousy seed. Depending on the value of $$, it might take a while before time|$$ actually produces a different value. And that results in getting the same value from rand. The fact your console program gives different values comes because $$ changes. But then you still should see the same number reappear every once in a while.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://430924]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-28 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found