
Randomizing a 24hr time period
Hello,
I'm trying to come up with a function to send out X number of message per
day(24hr day) but we need to randomize the time factore each day. For example if
50 messages are scheduled to be sent out in 24 hr time period we want to
randomize the times they go out during this time period instead of a fixed time
period.
So if a message goes out at 1:25am, the next message goes out at some random
time, the next message goes out at a random time, and so on. We will be setting
up a cron job to run a script every minute to send out these messages but don't
want to send them out a per determined times but to fully randomize the time
factor for each 24 time period(ea day).
Hope this makes sense, and someone may have done this before or may have some
info the I can look into.
Thx's
Mike(mickalo)Blezien
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
On 2/7/11 Mon Feb 7, 2011 1:30 PM, "Mike Blezien"
<mickalo [at] frontiernet.net> scribbled:
> Hello,
>
> I'm trying to come up with a function to send out X number of message per
> day(24hr day) but we need to randomize the time factore each day. For example
> if
> 50 messages are scheduled to be sent out in 24 hr time period we want to
> randomize the times they go out during this time period instead of a fixed
> time
> period.
>
> So if a message goes out at 1:25am, the next message goes out at some random
> time, the next message goes out at a random time, and so on. We will be
> setting
> up a cron job to run a script every minute to send out these messages but
> don't
> want to send them out a per determined times but to fully randomize the time
> factor for each 24 time period(ea day).
You can generate uniform random numbers between 0 and 1 by using the
built-in rand() function. See
perldoc -f rand
perldoc -q random:
"Why aren't my random numbers random?"
"How do I get a random number between X and Y?"
So, for example, you can calculate a randomized time for the next message in
the interval 1 to 59 with the expression (1 + int(rand(59)). The average
interval will be 30 minutes, which will result in, on average, 48 messages
per day. Some days will have more, some less. Intervals will range from 1 to
59 minutes. If that is too much variation, you can use a different interval,
e.g. 20 to 39, which still has an average interval of 30.
If you are starting an instance of your program every minute, you will have
to save the time for the next message and only send a message when that time
occurs, then compute the time for the next message.
You can also compute the probability of sending a message as 50/1440
(0.0347222), compute a random number every minute, and only send the message
if the random number is less than 0.0347222. You have no control over the
interval between successive messages that way, however.
If a uniform distribution is not desirable, see the Math::Random module for
more distributions. The normal ("gaussian") distribution ("bell curve") is
quite common in many applications.
If having exactly 50 messages in a 24-hour period is important, then you
will have to use a more adaptive probability calculator that looks at the
number of messages issued so far in calculating whether or not to send a
message at each minute.
Search for "probability distributions" for more information.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
------=_NextPart_000_0049_01CBC6F0.A3ECB1C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Thanks Jim, this helps allot. You've given me a good starting point. =
Appreciate the help.
Mike
----- Original Message -----
From: Jim Gibson
To: Perl List
Sent: Monday, February 07, 2011 5:52 PM
Subject: Re: Randomizing a 24hr time period
On 2/7/11 Mon Feb 7, 2011 1:30 PM, "Mike Blezien"
<mickalo [at] frontiernet.net> scribbled:
> Hello,
>
> I'm trying to come up with a function to send out X number of =
message per
> day(24hr day) but we need to randomize the time factore each day. =
For example
> if
> 50 messages are scheduled to be sent out in 24 hr time period we =
want to
> randomize the times they go out during this time period instead of a =
fixed
> time
> period.
>
> So if a message goes out at 1:25am, the next message goes out at =
some random
> time, the next message goes out at a random time, and so on. We will =
be
> setting
> up a cron job to run a script every minute to send out these =
messages but
> don't
> want to send them out a per determined times but to fully randomize =
the time
> factor for each 24 time period(ea day).
You can generate uniform random numbers between 0 and 1 by using the
built-in rand() function. See
perldoc -f rand
perldoc -q random:
"Why aren't my random numbers random?"
"How do I get a random number between X and Y?"
So, for example, you can calculate a randomized time for the next =
message in
the interval 1 to 59 with the expression (1 + int(rand(59)). The =
average
interval will be 30 minutes, which will result in, on average, 48 =
messages
per day. Some days will have more, some less. Intervals will range =
from 1 to
59 minutes. If that is too much variation, you can use a different =
interval,
e.g. 20 to 39, which still has an average interval of 30.
If you are starting an instance of your program every minute, you will =
have
to save the time for the next message and only send a message when =
that time
occurs, then compute the time for the next message.
You can also compute the probability of sending a message as 50/1440
(0.0347222), compute a random number every minute, and only send the =
message
if the random number is less than 0.0347222. You have no control over =
the
interval between successive messages that way, however.
If a uniform distribution is not desirable, see the Math::Random =
module for
more distributions. The normal ("gaussian") distribution ("bell =
curve") is
quite common in many applications.
If having exactly 50 messages in a 24-hour period is important, then =
you
will have to use a more adaptive probability calculator that looks at =
the
number of messages issued so far in calculating whether or not to send =
a
message at each minute.
Search for "probability distributions" for more information.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
------=_NextPart_000_0049_01CBC6F0.A3ECB1C0--
Re: Randomizing a 24hr time period
On Mon, Feb 07, 2011 at 03:30:03PM -0600, Mike Blezien wrote:
> I'm trying to come up with a function to send out X number of message per
> day(24hr day) but we need to randomize the time factore each day. For
<snip>
> Hope this makes sense, and someone may have done this before or may have
> some info the I can look into.
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> Thunder Rain Internet Publishing
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
This sure looks like a spammer asking for help.
What's the justification for this need?
Mike
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
------=_NextPart_000_000F_01CBC80A.333C2940
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
it isn't for spam I assure you. A client of ours sends out 25-50, =
messages/quotes, a day to various spiritual web sites. And they have =
already been approved by the web sites they are posted too. They just =
wanted a solution to randomize when the messages go out so they are like =
a "surprize" when they appear.
Mike(mickalo)Blezien
=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- =3D-=3D-=3D-=3D=
-=3D-=3D-=3D-=3D-=3D-=3D-=3D
Thunder Rain Internet Publishing
-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D -=3D-=3D-=3D-=
=3D-=3D-=3D-=3D-=3D-=3D-=3D-
----- Original Message -----
From: Mike McClain
To: Perl List
Sent: Monday, February 07, 2011 10:31 PM
Subject: Re: Randomizing a 24hr time period
On Mon, Feb 07, 2011 at 03:30:03PM -0600, Mike Blezien wrote:
> I'm trying to come up with a function to send out X number of =
message per
> day(24hr day) but we need to randomize the time factore each day. =
For
<snip>
> Hope this makes sense, and someone may have done this before or may =
have
> some info the I can look into.
> =
=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- =3D-=3D-=3D-=3D=
-=3D-=3D-=3D-=3D-=3D-=3D-=3D
> Thunder Rain Internet Publishing
> =
-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D -=3D-=3D-=3D-=
=3D-=3D-=3D-=3D-=3D-=3D-=3D-
>
This sure looks like a spammer asking for help.
What's the justification for this need?
Mike
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
------=_NextPart_000_000F_01CBC80A.333C2940--
Re: Randomizing a 24hr time period
>>>>> "MB" == Mike Blezien <mickalo [at] frontiernet.net> writes:
MB> it isn't for spam I assure you. A client of ours sends out 25-50,
MB> messages/quotes, a day to various spiritual web sites. And they
MB> have already been approved by the web sites they are posted
MB> too. They just wanted a solution to randomize when the messages go
MB> out so they are like a "surprize" when they appear.
given that email has random delays anyhow, why not just make them go out
on a fixed period? the receivers will never know it wasn't random. and
that is a lot of messages sent per day. if someone doesn't read email
often, they will see a bunch in their inbox at a time. no randomness
will be percieved there. i bet no one will ever say, they came in too
regularly but i wanted more random spacing!
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
------=_NextPart_000_004C_01CBC80F.2BC1B1D0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Uri,
yes that does make sense but that's what the customer wants :) There =
always right aren't they !!
Mike(mickalo)Blezien
=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- =3D-=3D-=3D-=3D=
-=3D-=3D-=3D-=3D-=3D-=3D-=3D
Thunder Rain Internet Publishing
-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D -=3D-=3D-=3D-=
=3D-=3D-=3D-=3D-=3D-=3D-=3D-
----- Original Message -----
From: Uri Guttman
To: Mike Blezien
Cc: Perl List ; Mike McClain
Sent: Wednesday, February 09, 2011 4:06 AM
Subject: Re: Randomizing a 24hr time period
>>>>> "MB" =3D=3D Mike Blezien <mickalo [at] frontiernet.net> writes:
MB> it isn't for spam I assure you. A client of ours sends out =
25-50,
MB> messages/quotes, a day to various spiritual web sites. And they
MB> have already been approved by the web sites they are posted
MB> too. They just wanted a solution to randomize when the messages =
go
MB> out so they are like a "surprize" when they appear.
given that email has random delays anyhow, why not just make them go =
out
on a fixed period? the receivers will never know it wasn't random. and
that is a lot of messages sent per day. if someone doesn't read email
often, they will see a bunch in their inbox at a time. no randomness
will be percieved there. i bet no one will ever say, they came in too
regularly but i wanted more random spacing!
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- =
http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support =
------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com =
---------
------=_NextPart_000_004C_01CBC80F.2BC1B1D0--
Re: Randomizing a 24hr time period
>>>>> "MB" == Mike Blezien <mickalo [at] frontiernet.net> writes:
MB> Uri,
MB> yes that does make sense but that's what the customer wants :)
MB> There always right aren't they !!
then do something very simple. send a message every N minutes where N is
randomly picked from say 5-10. they will never know it wasn't a more
random distribution.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
------=_NextPart_000_0069_01CBC819.1BD03A80
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
What they have right now are about 25 messages they wish to spread out =
through out a 24hr period so they are sent out randomly during this 24 =
period, not a pre determined every X minutes or X hours. Why .... I'm =
not really sure, but that's what they want to do. I think I have it =
figured out though.
thx's
Mike(mickalo)Blezien
=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- =3D-=3D-=3D-=3D=
-=3D-=3D-=3D-=3D-=3D-=3D-=3D
Thunder Rain Internet Publishing
-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D -=3D-=3D-=3D-=
=3D-=3D-=3D-=3D-=3D-=3D-=3D-
----- Original Message -----
From: Uri Guttman
To: Mike Blezien
Cc: Perl List ; Mike McClain
Sent: Wednesday, February 09, 2011 5:06 AM
Subject: Re: Randomizing a 24hr time period
>>>>> "MB" =3D=3D Mike Blezien <mickalo [at] frontiernet.net> writes:
MB> Uri,
MB> yes that does make sense but that's what the customer wants :)
MB> There always right aren't they !!
then do something very simple. send a message every N minutes where N =
is
randomly picked from say 5-10. they will never know it wasn't a more
random distribution.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- =
http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support =
------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com =
---------
------=_NextPart_000_0069_01CBC819.1BD03A80--
Re: Randomizing a 24hr time period
>>>>> "MB" == Mike Blezien <mickalo [at] frontiernet.net> writes:
MB> What they have right now are about 25 messages they wish to spread
MB> out through out a 24hr period so they are sent out randomly during
MB> this 24 period, not a pre determined every X minutes or X
MB> hours. Why .... I'm not really sure, but that's what they want to
MB> do. I think I have it figured out though.
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.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
On 07/02/2011 21:30, Mike Blezien wrote:
> Hello,
>
> I'm trying to come up with a function to send out X number of message per
> day(24hr day) but we need to randomize the time factore each day. For example if
> 50 messages are scheduled to be sent out in 24 hr time period we want to
> randomize the times they go out during this time period instead of a fixed time
> period.
>
> So if a message goes out at 1:25am, the next message goes out at some random
> time, the next message goes out at a random time, and so on. We will be setting
> up a cron job to run a script every minute to send out these messages but don't
> want to send them out a per determined times but to fully randomize the time
> factor for each 24 time period(ea day).
>
> Hope this makes sense, and someone may have done this before or may have some
> info the I can look into.
The program below may be some help.
Rob
use strict;
use warnings;
use List::Util qw/shuffle/;
my ($h, $m) = (0, 0);
my [at] times;
while ($h < 24) {
push [at] times, sprintf '%02d:%02d', $h, $m;
++$h, $m = 0 if ++$m >= 60;
}
my [at] sample = (shuffle [at] times)[0..24];
print "$_\n" foreach sort [at] sample;
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
----- Original Message -----
From: "Uri Guttman" <uri [at] StemSystems.com>
To: "Mike Blezien" <mickalo [at] frontiernet.net>
Cc: "Perl List" <beginners [at] perl.org>; "Mike McClain" <mike.junk [at] nethere.com>
Sent: Wednesday, February 09, 2011 9:35 AM
Subject: Re: Randomizing a 24hr time period
>>>>>> "MB" == Mike Blezien <mickalo [at] frontiernet.net> writes:
>
> MB> What they have right now are about 25 messages they wish to spread
> MB> out through out a 24hr period so they are sent out randomly during
> MB> this 24 period, not a pre determined every X minutes or X
> MB> hours. Why .... I'm not really sure, but that's what they want to
> MB> do. I think I have it figured out though.
>
> 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.
>
> uri
Uri,
that's what I'm working on at the moment, trying to randomize the intervals
after splitting up the 24hr period.
Thx's
Mike
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
New to this mailing list so this is my first response.
Why not just take (24*60)/[number of unsent messages] and send them on
that interval?
You probably dont want messages appearing in the dead of night, so you
can safely assume the last 2 hours and first 6 are off limits by doing
((16*60)/[number of unsent messages])+(6*60).
Your client probably wont get up at 6am just to send off 25 messages,
so another addition would be to get the current time and modify the
above formula to reflect the current time. This would work if they
came back to add a few more later messages after lunch, assuming
something is keeping track of which ones were sent and unsent.
(((22-[current hour])*60)/[number of unsent messages])+(([current
hour]-1)*60+[current minute]).
Again no randomness is needed because its likely the client wont send
them at the exact time every day with the exact same number.
Thats probably the most I can help you with it, post some code if you
want more help.
On 2/9/11, Mike Blezien <mickalo [at] frontiernet.net> wrote:
> What they have right now are about 25 messages they wish to spread out
> through out a 24hr period so they are sent out randomly during this 24
> period, not a pre determined every X minutes or X hours. Why .... I'm not
> really sure, but that's what they want to do. I think I have it figured out
> though.
>
> thx's
>
> Mike(mickalo)Blezien
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> Thunder Rain Internet Publishing
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> ----- Original Message -----
> From: Uri Guttman
> To: Mike Blezien
> Cc: Perl List ; Mike McClain
> Sent: Wednesday, February 09, 2011 5:06 AM
> Subject: Re: Randomizing a 24hr time period
>
>
> >>>>> "MB" == Mike Blezien <mickalo [at] frontiernet.net> writes:
>
> MB> Uri,
>
> MB> yes that does make sense but that's what the customer wants :)
> MB> There always right aren't they !!
>
> then do something very simple. send a message every N minutes where N is
> randomly picked from say 5-10. they will never know it wasn't a more
> random distribution.
>
> uri
>
> --
> Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com
> --
> ----- Perl Code Review , Architecture, Development, Training, Support
> ------
> --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com
> ---------
>
--
Skirv
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
>>>>> "MB" == Mike Blezien <mickalo [at] 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).
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
--0016364ef702ed5f88049bdbdd08
Content-Type: text/plain; charset=ISO-8859-1
On Wed, Feb 9, 2011 at 9:41 AM, Rob Dixon <rob.dixon [at] gmx.com> wrote:
> On 07/02/2011 21:30, Mike Blezien wrote:
>
>> I'm trying to come up with a function to send out X number of message per
>> day(24hr day) but we need to randomize the time factore each day. For
>> example if
>> 50 messages are scheduled to be sent out in 24 hr time period we want to
>> randomize the times they go out during this time period instead of a fixed
>> time
>> period.
>>
> <snip>
>
> The program below may be some help.
>
> Rob
>
That's an elegant solution. If I understand the code correctly, you're
treating the list of minutes in a day like a deck of cards. Each minute
represents one card. You shuffle the deck. The first 50 elements (cards) are
50 random times of the day.
while ($h < 24) {
> push [at] times, sprintf '%02d:%02d', $h, $m;
> ++$h, $m = 0 if ++$m >= 60;
> }
>
I assume that Mike (the original poster) would get 50 times by substituting
"[0..50]" in place of "[0..24]".
my [at] sample = (shuffle [at] times)[0..24];
>
And this last piece then sorts the 50 random times into chronological order
for the cron job. It's very clever.
print "$_\n" foreach sort [at] sample;
--
Robert Wohlfarth
--0016364ef702ed5f88049bdbdd08--
Re: Randomizing a 24hr time period
On Wed, Feb 09, 2011 at 11:16:07AM -0500, Uri Guttman wrote:
> >>>>> "MB" == Mike Blezien <mickalo [at] 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) = [at] ARGV;
# set up initial distribution
my [at] times = map rand, 1 .. $messages;
my $duration; $duration += $_ for [at] times;
# spread over required time period
my $factor = $time_period / $duration; $_ *= $factor for [at] times;
# calculate actual times
my $time = 0; $time = $_ += $time for [at] times;
# shift down
my $shift = rand $times[0]; $_ -= $shift for [at] times;
my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for [at] times;
--
Paul Johnson - paul [at] pjcj.net
http://www.pjcj.net
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
----- Original Message -----
From: "Paul Johnson" <paul [at] pjcj.net>
To: "Uri Guttman" <uri [at] StemSystems.com>
Cc: "Mike Blezien" <mickalo [at] frontiernet.net>; "Perl List" <beginners [at] 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 <mickalo [at] 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) = [at] ARGV;
>
> # set up initial distribution
> my [at] times = map rand, 1 .. $messages;
> my $duration; $duration += $_ for [at] times;
>
> # spread over required time period
> my $factor = $time_period / $duration; $_ *= $factor for [at] times;
>
> # calculate actual times
> my $time = 0; $time = $_ += $time for [at] times;
>
> # shift down
> my $shift = rand $times[0]; $_ -= $shift for [at] times;
>
> my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for [at] times;
Paul,
this works quiet well too. Appreciate the code, it save me allot time :)
Mike
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
----- Original Message -----
From: "Paul Johnson" <paul [at] pjcj.net>
To: "Uri Guttman" <uri [at] StemSystems.com>
Cc: "Mike Blezien" <mickalo [at] frontiernet.net>; "Perl List" <beginners [at] 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 <mickalo [at] 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) = [at] ARGV;
>
> # set up initial distribution
> my [at] times = map rand, 1 .. $messages;
> my $duration; $duration += $_ for [at] times;
>
> # spread over required time period
> my $factor = $time_period / $duration; $_ *= $factor for [at] times;
>
> # calculate actual times
> my $time = 0; $time = $_ += $time for [at] times;
>
> # shift down
> my $shift = rand $times[0]; $_ -= $shift for [at] times;
>
> my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for [at] 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 [at] 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?
Thanks,
Mike
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
On 2/9/11 Wed Feb 9, 2011 1:05 PM, "Mike Blezien"
<mickalo [at] frontiernet.net> scribbled:
> ----- Original Message -----
> From: "Paul Johnson" <paul [at] pjcj.net>
> To: "Uri Guttman" <uri [at] StemSystems.com>
> Cc: "Mike Blezien" <mickalo [at] frontiernet.net>; "Perl List" <beginners [at] 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 <mickalo [at] 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) = [at] ARGV;
>>
>> # set up initial distribution
>> my [at] times = map rand, 1 .. $messages;
>> my $duration; $duration += $_ for [at] times;
>>
>> # spread over required time period
>> my $factor = $time_period / $duration; $_ *= $factor for [at] times;
>>
>> # calculate actual times
>> my $time = 0; $time = $_ += $time for [at] times;
>>
>> # shift down
>> my $shift = rand $times[0]; $_ -= $shift for [at] times;
>>
>> my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for [at] 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 [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
----- Original Message -----
From: "Jim Gibson" <jimsgibson [at] gmail.com>
To: "Perl List" <beginners [at] perl.org>
Sent: Wednesday, February 09, 2011 4:04 PM
Subject: Re: Randomizing a 24hr time period
> On 2/9/11 Wed Feb 9, 2011 1:05 PM, "Mike Blezien"
> <mickalo [at] frontiernet.net> scribbled:
>
>> ----- Original Message -----
>> From: "Paul Johnson" <paul [at] pjcj.net>
>> To: "Uri Guttman" <uri [at] StemSystems.com>
>> Cc: "Mike Blezien" <mickalo [at] frontiernet.net>; "Perl List"
>> <beginners [at] 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 <mickalo [at] 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) = [at] ARGV;
>>>
>>> # set up initial distribution
>>> my [at] times = map rand, 1 .. $messages;
>>> my $duration; $duration += $_ for [at] times;
>>>
>>> # spread over required time period
>>> my $factor = $time_period / $duration; $_ *= $factor for [at] times;
>>>
>>> # calculate actual times
>>> my $time = 0; $time = $_ += $time for [at] times;
>>>
>>> # shift down
>>> my $shift = rand $times[0]; $_ -= $shift for [at] times;
>>>
>>> my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for [at] 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 [at] 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);
Ok how would I go about getting this variable from this line or I'm I missing
something:
my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for [at] times;
instead of printing it output ? This where I get messed up.
Thx's
Mike
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
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) = [at] ARGV;
>
> # set up initial distribution
> my [at] times = map rand, 1 .. $messages;
> my $duration; $duration += $_ for [at] times;
>
> # spread over required time period
> my $factor = $time_period / $duration; $_ *= $factor for [at] times;
>
> # calculate actual times
> my $time = 0; $time = $_ += $time for [at] times;
>
> # shift down
> my $shift = rand $times[0]; $_ -= $shift for [at] times;
>
> my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
On 2/9/11 Wed Feb 9, 2011 2:30 PM, "Mike Blezien"
<mickalo [at] frontiernet.net> scribbled:
> ----- Original Message -----
> From: "Jim Gibson" <jimsgibson [at] gmail.com>
> To: "Perl List" <beginners [at] perl.org>
> Sent: Wednesday, February 09, 2011 4:04 PM
> Subject: Re: Randomizing a 24hr time period
>
>
>> On 2/9/11 Wed Feb 9, 2011 1:05 PM, "Mike Blezien"
>> <mickalo [at] frontiernet.net> scribbled:
>>
>>> ----- Original Message -----
>>> From: "Paul Johnson" <paul [at] pjcj.net>
>>>
>>> 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 [at] 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);
>
> Ok how would I go about getting this variable from this line or I'm I missing
> something:
>
> my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for [at] times;
>
> instead of printing it output ? This where I get messed up.
That statement is a little complex for a beginner's list (IMO), as is the
rest of Paul's program.
That one line above is equivalent to the following:
my $n;
for ( [at] times ) {
printf( "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60);
}
You are only interested in the hour:minute part of the output, so you can
ignore the counter $n, and for multi-line loops you should use an explicit
loop variable instead of the default $_, giving:
for my $hr ( [at] times ) {
my $hhmm = sprintf( "%02d:%02d", $hr, ($hr - int $hr) * 60);
# do something with $hhmm
}
The [at] times array contains numerical values in hours, so $hhmm will contain a
string of the form "23:59", depending upon the values entered for the number
of messages and the duration.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
----- Original Message -----
From: "Jim Gibson" <jimsgibson [at] gmail.com>
To: "Perl List" <beginners [at] perl.org>
Sent: Wednesday, February 09, 2011 6:15 PM
Subject: Re: Randomizing a 24hr time period
> On 2/9/11 Wed Feb 9, 2011 2:30 PM, "Mike Blezien"
> <mickalo [at] frontiernet.net> scribbled:
>
>> ----- Original Message -----
>> From: "Jim Gibson" <jimsgibson [at] gmail.com>
>> To: "Perl List" <beginners [at] perl.org>
>> Sent: Wednesday, February 09, 2011 4:04 PM
>> Subject: Re: Randomizing a 24hr time period
>>
>>
>>> On 2/9/11 Wed Feb 9, 2011 1:05 PM, "Mike Blezien"
>>> <mickalo [at] frontiernet.net> scribbled:
>>>
>>>> ----- Original Message -----
>>>> From: "Paul Johnson" <paul [at] pjcj.net>
>
>>>>
>>>> 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 [at] 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);
>>
>> Ok how would I go about getting this variable from this line or I'm I missing
>> something:
>>
>> my $n; printf "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60 for [at] times;
>>
>> instead of printing it output ? This where I get messed up.
>
> That statement is a little complex for a beginner's list (IMO), as is the
> rest of Paul's program.
>
> That one line above is equivalent to the following:
>
> my $n;
> for ( [at] times ) {
> printf( "%2d. %02d:%02d\n", ++$n, $_, ($_ - int $_) * 60);
> }
>
> You are only interested in the hour:minute part of the output, so you can
> ignore the counter $n, and for multi-line loops you should use an explicit
> loop variable instead of the default $_, giving:
>
> for my $hr ( [at] times ) {
> my $hhmm = sprintf( "%02d:%02d", $hr, ($hr - int $hr) * 60);
> # do something with $hhmm
> }
>
> The [at] times array contains numerical values in hours, so $hhmm will contain a
> string of the form "23:59", depending upon the values entered for the number
> of messages and the duration.
thanks Jim. After playing around with it for a while finally figured it myself.
just had look at a bit more. been out of the programming game for a while and
still rusty. but it's coming back. Thanks to all the other help that was posted.
Mike
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Randomizing a 24hr time period
On Wed, Feb 09, 2011 at 10:52:24PM +0000, Rob Dixon wrote:
> 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:
> 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.
Hello Rob,
Yes, sorry, that wasn't very clear.
What I meant was that Uri's solution is simple, but clearly solving a
slightly different problem. Your solution is elegant and obviously
correct, with every minute having an equal probability of being
selected. My solution is more difficult to reason about, and my brain
is telling me that probability is hard, let's go shopping. (Hence the
parenthetical "more".)
Then again, why shouldn't two or more messages be sent out at the same
minute? Perhaps all that's really needed is:
perl -E 'say for sort map { sprintf "%02d:%02d", rand 24, rand 60 } 1 .. shift' 50
--
Paul Johnson - paul [at] pjcj.net
http://www.pjcj.net
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/