On 09/02/2011 17:05, Paul Johnson wrote:
Of course you can redefine the problem this way, but it's more interesting to solve the original problem. To go with Rob's solution, here's something that's (more) correct: #!/usr/bin/perl use strict; use warnings; my ($messages, $time_period) = @ARGV; # set up initial distribution my @times = map rand, 1 .. $messages; my $duration; $duration += $_ for @times; # spread over required time period my $factor = $time_period / $duration; $_ *= $factor for @times; # calculate actual times my $time = 0; $time = $_ += $time for @times; # shift down my $shift = rand $times[0]; $_ -= $shift for @times; my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for @times;
Hey Paul I'm not clear whether you meant your solution was 'more correct' than mine or than Uri's? Of course it doesn't matter at all given the triviality of the requirement, but my program produced N independent but different minutes of the day, whereas yours results in times that are, on average, spaced equally over the day. I don't think that can be said to be more accurate than anything else that has been suggested. Cheers, Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/