----- 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,

this works quiet well too. Appreciate the code, it save me allot time :)

Mike

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to