On 2/9/11 Wed Feb 9, 2011 1:05 PM, "Mike Blezien" <mick...@frontiernet.net> scribbled:
> ----- Original Message ----- > From: "Paul Johnson" <p...@pjcj.net> > To: "Uri Guttman" <u...@stemsystems.com> > Cc: "Mike Blezien" <mick...@frontiernet.net>; "Perl List" <beginners@perl.org> > Sent: Wednesday, February 09, 2011 11:05 AM > Subject: Re: Randomizing a 24hr time period > > >> On Wed, Feb 09, 2011 at 11:16:07AM -0500, Uri Guttman wrote: >>>>>>>> "MB" == Mike Blezien <mick...@frontiernet.net> writes: >>> >>>>> as i said a simple solution is to slice up the 24 hours into fixed >>>>> intervals. then pick a random time INSIDE each interval. random enough >>>>> for those types of people. this is close to one message an hour so there >>>>> is plenty of variability within each hour. and the coding is trivial. >>> >>> MB> Uri, >>> >>> MB> that's what I'm working on at the moment, trying to randomize the >>> MB> intervals after splitting up the 24hr period. >>> >>> so what is taking so long? it is about 2 lines of code! seriously, just >>> do int( rand( 60 ) and send it out that minute within the hour. (adjust >>> 60 for the actual period from splitting the day). >> >> "For every complex problem there is an answer that is clear, simple, and >> wrong." H. L. Mencken. >> >> 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; >> > > Paul, > > quick question. my perl is a bit rusty, been away from it for awhile. But this > line in your coding: > > my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for @times; > > I need to put the code: %02d:%02d into a variable to store it in a > database, the hour:minute, how would I go about putting them into a variable? "%02:%02d" is not code as such. It is part of the format specifier for the printf function. You can use a string variable for the format specifier: my $fmt = "%02d:%02d"; print $fmt, $hour, $min; If that is not what you want, then maybe you want to put the string that is printed according to the specified format into a variable instead of writing to an output stream. To do that, use the sprintf function instead of printf: my $hhmm = sprintf("%02:%02",$hour,$min); See (as always): perldoc -f printf perldoc -f sprintf -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/