in reply to Re: Monte Carlo - Coin Toss
in thread Monte Carlo - Coin Toss
It is more idiomatic (and often simpler) to use Perl-style rather than C-style loops.
Yes, but it won't work if the numbers are too large for integers and have to use floating point. Then only C style loops will work.
my @collect = map 0, 0 .. NUM_TOSSES;
Why are you initializing @collect with NUM_TOSSES + 1 elements?
The idiomatic way to initialize @collect with NUM_TOSSES elements is usually:
my @collect = ( 0 ) x NUM_TOSSES;
for ($i = 0; $i < $numTosses+1; $i++) { foreach my $tailsCt ( 0 .. NUM_TOSSES )
You have an off-by-one error, it should be:
foreach my $tailsCt ( 0 .. NUM_TOSSES - 1 )
Update: oops, I misread the original code.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Monte Carlo - Coin Toss
by johngg (Canon) on Mar 12, 2011 at 12:38 UTC | |
Re^3: Monte Carlo - Coin Toss
by Eliya (Vicar) on Mar 12, 2011 at 10:40 UTC | |
Re^3: Monte Carlo - Coin Toss
by ikegami (Patriarch) on Mar 12, 2011 at 08:18 UTC |
In Section
Cool Uses for Perl