Jump to content

best way to approach random time blocks with break periods in a day


Go to solution Solved by Barand,

Recommended Posts

I am trying to wrap my head around the best approach to accomplish this.

 

I am trying to come up with a random schedule for a full 24 hour day with random time periods ranges including breaks.

The only constants will be the minimum time and maximum time of the events and the minimum and maximum time of the breaks between each event.

 

for example

There are three teams

Team A

Team B

Team C

 

Each event will last either a minimum of 7 minutes to a maximum of 38 minutes

The breaks will last a minimum of 30 seconds to a maximum of 5 minutes.

 

so the events need to be generated randomly with a break period following each event and the timelines are different for each team

with the current event time left going to  a timer on the page

 

I think I have the part to generate the random blocks figured out by using 

<!DOCTYPE html>
<html>
<body>
<?php
function eventRange($min, $max, $blocks) {
    $events = range($min, $max);
    shuffle($events);    
    return array_slice($events, 0, $blocks);
    
}

function breakRange($min, $max, $blocks) {
    $breaks = range($min, $max, 0.1);
    shuffle($breaks);    
    return array_slice($breaks, 0, $blocks);
    
}
echo"<pre>";
print_r( eventRange(7,38,32) );
echo"</pre>";

echo"<pre>";
print_r( breakRange(.5,5,32) );
echo"</pre>";

?>
</body>
</html>

bur is there a way to make it fill a full 24 hour period and then a way to determine which block to display based on the current time when either team is viewing the page.

 

Sounds totally confusing to me as I try to explain it so I hope what Im asking makes sense. Thanks for any guidance..

 

 

 

 

 

Each event involves a single team but each team will eventually do each event if that makes sense

 

Along the lines of a relay race sort of...

 

Each team starts at a different station (event) has to complete it in that time-frame and has the break period until going on to the next station (event).

No Guess I should have explained that better

 

for instance

 

Event 1 = 7 minutes / break after Event 1 = 2 minutes 30 seconds

Event 2 = 24 minutes  / break after Event 2 = 3 minutes 24 seconds

Event 3 = 9 minutes 14 seconds / break after Event 3 = 1 minute

and so on until the times (Events + Breaks) fill a 24 hour period

 

The number of events and number of breaks do not matter, it is determined from the minimum and maximum set for the ranges of both events and breaks

but has to fill a 24 hour period

 

Then each team will  complete each event just in different orders

So Team 1

might do Event 3 then go to Event 1 then to Event 2

 

Team 2

might do Event 2 then go to Event 3 then to Event 1

 

Team 3

might do Event 3 then go to Event 2 then to Event 1

 

and so on

--------

All the events run simultaneous from 12:00a.m. to 11:59p.m.

but each team will do them in different orders

some overlap is ok but I cant have all teams doing each event at the same time/order.

 

Hopefully that makes more sense.

That's a lot clearer.

 

From the times produced by your code you will have around 57 events

$e = eventRange(7,38,32);
$b =  breakRange(.5,5,32);
$ae = array_sum ($e) / count($e);
$ab = array_sum($b) / count($b);
$k = floor((24*60)/($ae+$ab));

echo"<pre>";
print_r($e);
print_r($b );
printf ("Ave event: %5.2f\nAve break: %5.2f\nNum events in 24hrs: %d", $ae, $ab, $k);
echo"</pre>";

Giving:

Ave event: 22.50
Ave break:  2.76
Num events in 24hrs: 57

You have set a restriction that all events must be of different durations (and the same for the breaks). Does that need to be the case?

Edited by Barand
  • Solution

I would generate random times for the events and breaks and maintain a running total of the time until 24 hours were filled

$events = array();
$cumval = 0;
$finished = 0;
$k = 0;

//
// GENERATE RANDOM EVENT AND BREAK TIMES
// UNTIL THE 24HRS HAVE BEEN FILLED
//

while (!$finished) {
    
    $remain = 24*60*60 - $cumval;
    
    if ($remain < 7*60 + 30) 
        break; // cannot fit another event/break
    
    if ($remain < 39*60) {
        $eTime = floor($remain/60) - 1;
        $bTime = $remain - $eTime*60;
        $finished = 1;
    }
    else {
        $eTime = mt_rand(7, 38); // mins
        $bTime = mt_rand(3, 30) * 10; // 10 sec increments
    }
    
    $events[++$k] = array (
                    'event' => $eTime,    //mins
                    'break' => $bTime     //secs
                );
    
    $cumval += $eTime*60 + $bTime;
}

//
// GET THE SCHEDULE FOR EACH OF THE 3 TEAMS
//
$schedule = array();
$keys = array_keys($events);
$k = count($keys);
for ($team = 1; $team <= 3; $team++) {
    shuffle($keys);
    $schedule[$team] = $keys;
}

Once you have the event and break durations in the $events array you can generate a timetable for each time using their schedule of events

Thanks for that Barand!!

 

I was working on this a good part of today and came up with a method

The downfall to mine is I have to adjust the last element of the array to get it to the 1440 (Minutes of the day)

so I didnt like that.

//events
$events = range(7, 38, 0.01);
shuffle($events);

//breaks
$breaks = range(0.5, 5.5, 0.01);
shuffle($breaks);

$ae = array_sum ($events) / count($events);
$ab = array_sum($breaks) / count($breaks);
$k = floor((24*60)/($ae+$ab));
$theDay = array();
$c=0;
while ($c <= $k) {
    $c++;
    $theDay[]=$events[$c];
    $theDay[]=$breaks[$c];
}

if(array_sum($theDay) != 1440){
    if(array_sum($theDay) < 1440){        
        $diff = (1440-array_sum($theDay));
        $theDay[]=$diff;
    }
    if(array_sum($theDay) > 1440){
        while (array_sum($theDay) > 1440) {
            $num=count($theDay);
            $num=$num-1;
            unset($theDay[$num]);        
        }
        $diff = (1440-array_sum($theDay));
        $theDay[]=$diff;        
    }
}

I tested yours and it smooth but on some teams it falls shy of the 1440 so I have to see how that pans out when I try and figure out how to deploy these results to a timer.

 

I appreciate you taking the time and thanks for the first post because it got me thinking and trying different things.

My attempt seems alot less efficient than yours but I was trying hahaha

Thanks again!

Another thing that you might want to consider (and which would probably be appreciated by team member) is to keep the task time random but make the following break proportional to the task time so they they get a longer break after a long task.

$eTime = mt_rand(7, 38); // mins
$bTime = calcBreak(30,300,7,38,$eTime);

function calcBreak($min,$max,$minet,$maxet,$et)
{
    return floor(($max-$min) * ($et-$minet) / ($maxet-$minet)) + $min;
}

I might be able to work that in as a comparison of sorts at the end.

 

The actual "randomness"  is the key.

The goal of the exercise is for a team to review the schedule and dedicate their teams resources accordingly.

Measuring that ability is what I am after.

 

The events are made up of various logic and physical activities so they need to be able to assign the right people at the right times taking those breaks into consideration.

Mainly Im measuring the ability to put together the right team for the given task

actually completing the tasks is secondary if that makes sense.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.