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;
}