On 2/7/11 Mon Feb 7, 2011 1:30 PM, "Mike Blezien" <mick...@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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/