有没有办法根据百分比提取值?
价值概率:
差:1%
正常:29%
好:70%
$.datepicker.regional['es'] = {
closeText: 'Cerrar',
prevText: '< Ant',
nextText: 'Sig >',
currentText: 'Hoy',
monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
dayNames: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mié', 'Juv', 'Vie', 'Sáb'],
dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sá'],
weekHeader: 'Sm',
dateFormat: 'dd/mm/yy',
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: ''
};
$.datepicker.setDefaults($.datepicker.regional['es']);
$(function() {
$("#fecha").datepicker();
});
$(function() {
var fechasdisponibles = ["4-1-2017", "4-8-2017", "4-15-2017", "4-22-2017", "4-29-2017", "5-6-2017", "5-13-2017", "5-20-2017", "5-27-2017", "6-3-2017", "6-10-2017", "6-17-2017", "6-24-2017",
"7-1-2017", "7-8-2017", "7-15-2017", "7-22-2017", "7-29-2017", "8-5-2017", "8-12-2017", "8-19-2017", "8-26-2017", "9-2-2017", "9-9-2017", "9-16-2017", "9-23-2017",
"9-30-2017", "10-7-2017", "10-14-2017", "10-21-2017", "10-28-2017", "11-4-2017", "11-18-2017", "12-2-2017", "12-9-2017", "12-16-2017", "12-23-2017", "12-30-2017",
"1-6-2018", "1-20-2018", "2-3-2018", "2-17-2018", "3-3-2018", "3-17-2018", "3-31-2018"
];
function habilitardias(date) {
var sdate = $.datepicker.formatDate('m-d-yy', date)
console.log(sdate)
if ($.inArray(sdate, fechasdisponibles) != -1) {
return [true];
}
return [false];
}
$("#calendario").datepicker({
beforeShowDay: habilitardias,
dateFormat: 'DD, d MM, yy',
firstDay: 7,
defaultDate: 4,
onSelect: function() {
var dateText = $.datepicker.formatDate("dd-mm-yy", $(this).datepicker("getDate"));
var fechaselecc = dateText;
var diamesyear = fechaselecc.split("-");
alert("Ha selecionado la fecha de salida: " + diamesyear + " Itinerario actualizado");
var undiasmasuno = new Date(parseInt(
diamesyear[2], 10),
parseInt(diamesyear[1], 10) - 1,
parseInt(diamesyear[0], 10)
);
$('#date-start-output').html(undiasmasuno + " ");
undiasmasuno.setDate(undiasmasuno.getDate() + 1);
$('#date-start-output2').html(undiasmasuno + " ");
undiasmasuno.setDate(undiasmasuno.getDate() + 1);
$('#date-start-output3').html(undiasmasuno + " ");
undiasmasuno.setDate(undiasmasuno.getDate() + 1);
$('#date-start-output4').html(undiasmasuno + " ");
undiasmasuno.setDate(undiasmasuno.getDate() + 1);
$('#date-start-output5').html(undiasmasuno + " ");
undiasmasuno.setDate(undiasmasuno.getDate() + 1);
$('#date-start-output6').html(undiasmasuno + " ");
undiasmasuno.setDate(undiasmasuno.getDate() + 1);
$('#date-start-output7').html(undiasmasuno + " ");
undiasmasuno.setDate(undiasmasuno.getDate() + 1);
$('#date-start-output8').html(undiasmasuno + " ");
undiasmasuno.setDate(undiasmasuno.getDate() + 1);
},
});
}
);
</script>
<body>
<div type="text" id="calendario" name="calendario"></div>
<strong><span id="date-start-output" class="date start" style="color: #ff0000;">Día 1º: </span><span style="color: #ff0000;">MÉXICO-MADRID (Sábado)</span></strong><br> Salida en vuelo internacional hacia Madrid.<br>
<strong><span id="date-start-output2" class="imprimevari" style="color: #ff0000;">Día 2º: </span><span style="color: #ff0000;">MÉXICO-MADRID)</span></strong><br> Salida en vuelo internacional hacia Madrid.<br>
<strong><span id="date-start-output3" class="imprimevari" style="color: #ff0000;">Día 2º: </span><span style="color: #ff0000;">MÉXICO-MADRID)</span></strong><br> Salida en vuelo internacional hacia Madrid.<br>
<strong><span id="date-start-output4" class="imprimevari" style="color: #ff0000;">Día 2º: </span><span style="color: #ff0000;">MÉXICO-MADRID)</span></strong><br> Salida en vuelo internacional hacia Madrid.<br>
</body>
一个简单的条件陈述:
var move ["bad","normal","good"];
那么,对于这类问题,PHP比Javascript更好吗?
答案 0 :(得分:1)
您可以编写一个以给定概率百分比对值进行采样的函数:
function weightedSample(pairs) {
const n = Math.random() * 100;
const match = pairs.find(({value, probability}) => n <= probability);
return match ? match.value : last(pairs).value;
}
function last(array) {
return array[array.length - 1];
}
const result = weightedSample([
{value: 'Bad', probability: 1},
{value: 'Normal', probability: 29},
{value: 'Good', probability: 70}
]);
console.log(result);
我不能说PHP会更好。这种问题在PHP中不应该更难/更难。您应该使用哪些(JS或PHP)实际上取决于该功能是否应该在服务器或客户端上运行。
答案 1 :(得分:0)
我建议使用连续检查概率和随机数的其余部分。
此函数首先将返回值设置为最后一个可能的索引并迭代,直到随机值的其余部分小于实际概率。
概率必须加到一。
function getRandomIndexByProbability(probabilities) {
var r = Math.random(),
index = probabilities.length - 1;
probabilities.some(function (probability, i) {
if (r < probability) {
index = i;
return true;
}
r -= probability;
});
return index;
}
var i,
move = ["bad", "normal", "good"],
probabilities = [0.01, 0.29, 0.7],
count = {},
index;
move.forEach(function (a) { count[a] = 0; });
for (i = 0; i < 1e6; i++) {
index = getRandomIndexByProbability(probabilities);
count[move[index]]++;
}
console.log(count);
.as-console-wrapper { max-height: 100% !important; top: 0; }