Assign hours with a random with priodities

1

After searching on Google and not finding anything like that.

It occurred to me to make an array of this style

$orden= array(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12);

and with the function mt_rand() generate the random number:

$longitud = count($orden);    
$hora = mt_rand(0,$longitud);

This would return the position of the selected time.

But I think the solution provided by @Ricardo D. Quiroga is more appropriate.

I have the following table:

where I have to randomly assign a few hours of the day, but each hour has a different priority and a percentage chance of that particular time. The highest priority is 1 and the lowest is 5.

I know that the function rand (), generates a random number, defined by 2 parameters to determine between what range of numbers you want to generate the random number.

Is there any function in PHP to indicate priorities and the percentage of possibilities that comes out?

With the response of @Ricardo D. Quiroga I have created a function and modified it a bit, so that it becomes clearer to me:

function horas() {
    // calcular la prioridad
    $rand = mt_rand() / mt_getrandmax(); //generar un maldito numero entre 0 y 1
    $prioridad = 0;
    if ($rand < 0.4) { //equivale a 40% probabilidad
        $prioridad = 1;
    } elseif ($rand < 0.7) { // equivale a 30% probabilidad
        $prioridad = 2;
    } elseif ($rand < 0.85) {
        $prioridad = 3;
    } elseif ($rand < 0.95) {
        $prioridad = 4;
    } else { // 5% probabilidad restante
        $prioridad = 5;
    }   

    $tablaHorarioPrioridad = array(
    array("20:00", "21:00", "22:00"), 
        array("17:00", "18:00", "19:00"),
        array("15:00", "16:00"),
        array("12:00", "13:00"),
        array("10:00", "11:00")
    );

    $prioridad1 = $prioridad - 1;
    $longitud = count($tablaHorarioPrioridad[$prioridad1]);     
    $longitud1 = $longitud - 1;

    $posicion = rand(0, $longitud1);        
    $hora = $tablaHorarioPrioridad[$prioridad1][$posicion];

    return $hora;
}
    
asked by Isaac Palacio 16.02.2018 в 13:08
source

1 answer

4

Hello, the first thing I see is a probability problem, in the graph, since simply the sum of all the probabilities can not give more than 1, in your case, I suppose you have several grouping according to priority and based on the priority it is indifferent the schedule to choose that matches that priority.

in summary: for example, priority 5 with 5% grouped to (10:00, 11:00)

So we should first calculate the group based on the priority

// calcular la prioridad
$rand = mt_rand() / mt_getrandmax(); //generar un número entre 0 y 1
$prioridad = 0;
if ($rand < 0.4) { //equivale a 40% probabilidad
    $prioridad = 1;
} elseif ($rand < 0.7) { // equivale a 30% probabilidad
    $prioridad = 2;
} elseif ($rand < 0.85) {
    $prioridad = 3;
} elseif ($rando < 0.95) {
    $prioridad = 4;
} else { // 5% probabilidad restante
    $prioridad = 5;
}

Knowing the priority, we will only have to choose a time between those that comply with having the same priority, to simplify the search I put them in a two-dimensional array where the first index defines the priority.

$tablaHorarioPrioridad = array(
    array(20, 21, 22), 
    array(17, 18, 19),
    array(15, 16),
    array(12, 13),
    array(10, 11),
);

and good the final calculation is to choose a number from that list, all have equal probability for what we do:

$horario = $tablaHorarioPrioridad[$prioridad - 1][rand(0, count($tablaHorarioPrioridad[$prioridad - 1] - 1))];
  

Comments For some strange reason that I do not finish understanding PHP does not have a uniform function in the ranges [0, 1) generated by pseudo random numbers, to have something similar I had to resort to the contraption mt_rand() / mt_getrandmax() to have an approximation of this function which is essential for calculation of any type of probability.

Greetings

    
answered by 16.02.2018 / 15:03
source