29/05/2014, 14:03
|
| Colaborador | | Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 16 años, 5 meses Puntos: 1012 | |
Respuesta: Numeros aleatorios sin repetir la manera de "asegurar" que no habrá repetición es usar el algorismo de fisher-yates Cita: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Azar1</title>
<style type="text/css">
th {
width: 30px;
}
</style>
<script type="text/javascript">
function $(ids) {
return document.getElementById(ids);
}
function resetear() {
var tb = document.getElementsByTagName('tbody');
for (var t = 0, trs = tb[0].getElementsByTagName('tr'); t < trs.length; t++) {
for (var n = 1, tds = trs[t].getElementsByTagName('td'); n < tds.length; n++) {
tds[n].innerHTML = 0;
}
}
document.getElementById('iteracion').value = 0;
j = x = 1;
}
var tmpArray1 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];
Array.prototype.shuffle1 = function (){
var i = this.length, j, temp;
if (i == 0) return;
while (--i) {
j = Math.floor( Math.random() * i);
temp = this[i];
this[i] = this[j];
this[j] = temp;
}
$(this[0]+'0').innerHTML = parseInt($(this[0]+'0').innerHTML) + 1;
$(this[1]+'1').innerHTML = parseInt($(this[1]+'1').innerHTML) + 1;
$(this[2]+'2').innerHTML = parseInt($(this[2]+'2').innerHTML) + 1;
$(this[3]+'3').innerHTML = parseInt($(this[3]+'3').innerHTML) + 1;
$(this[4]+'4').innerHTML = parseInt($(this[4]+'4').innerHTML) + 1;
$(this[5]+'5').innerHTML = parseInt($(this[5]+'5').innerHTML) + 1;
$(this[6]+'6').innerHTML = parseInt($(this[6]+'6').innerHTML) + 1;
$(this[7]+'7').innerHTML = parseInt($(this[7]+'7').innerHTML) + 1;
$(this[8]+'8').innerHTML = parseInt($(this[8]+'8').innerHTML) + 1;
$(this[9]+'9').innerHTML = parseInt($(this[9]+'9').innerHTML) + 1;
};
var tmpArray2 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];
Array.prototype.shuffle2 = function (){
var n = this.length*3;
this.sort(
function() {
return Math.round(Math.random() * n--);
}
);
$(this[0]+'0').innerHTML = parseInt($(this[0]+'0').innerHTML) + 1;
$(this[1]+'1').innerHTML = parseInt($(this[1]+'1').innerHTML) + 1;
$(this[2]+'2').innerHTML = parseInt($(this[2]+'2').innerHTML) + 1;
$(this[3]+'3').innerHTML = parseInt($(this[3]+'3').innerHTML) + 1;
$(this[4]+'4').innerHTML = parseInt($(this[4]+'4').innerHTML) + 1;
$(this[5]+'5').innerHTML = parseInt($(this[5]+'5').innerHTML) + 1;
$(this[6]+'6').innerHTML = parseInt($(this[6]+'6').innerHTML) + 1;
$(this[7]+'7').innerHTML = parseInt($(this[7]+'7').innerHTML) + 1;
$(this[8]+'8').innerHTML = parseInt($(this[8]+'8').innerHTML) + 1;
$(this[9]+'9').innerHTML = parseInt($(this[9]+'9').innerHTML) + 1;
};
var x = 1, i1 = '';
function fncFY() {
document.getElementById('iteracion').value = x;
tmpArray2.shuffle1();
if (x >= 100) {
clearTimeout(i1);
} else {
x++;
i1 = setTimeout(function() {fncFY()}, 100);
}
}
var j = 1, i2 = '';
function fncSort() {
document.getElementById('iteracion').value = j;
tmpArray2.shuffle2();
if (j >= 100) {
clearTimeout(i2);
} else {
j++;
i2 = setTimeout(function() {fncSort()}, 100);
}
}
</script>
</head>
<body>
<table cellspadding="10px">
<thead>
<tr>
<td colspan="11">
<div>
<button onclick="fncSort()">BARAJAR con SORT</button>
<button onclick="fncFY()">BARAJAR con Fisher–Yates</button>
<button onclick="resetear()">Resetear</button>
</td>
</tr>
<tr>
<th><input id="iteracion" value="0"></th>
</tr>
<tr>
<th>LETRAS</th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td id="A0">0</td>
<td id="A1">0</td>
<td id="A2">0</td>
<td id="A3">0</td>
<td id="A4">0</td>
<td id="A5">0</td>
<td id="A6">0</td>
<td id="A7">0</td>
<td id="A8">0</td>
<td id="A9">0</td>
</tr>
<tr>
<td>B</td>
<td id="B0">0</td>
<td id="B1">0</td>
<td id="B2">0</td>
<td id="B3">0</td>
<td id="B4">0</td>
<td id="B5">0</td>
<td id="B6">0</td>
<td id="B7">0</td>
<td id="B8">0</td>
<td id="B9">0</td>
</tr>
<tr>
<td>C</td>
<td id="C0">0</td>
<td id="C1">0</td>
<td id="C2">0</td>
<td id="C3">0</td>
<td id="C4">0</td>
<td id="C5">0</td>
<td id="C6">0</td>
<td id="C7">0</td>
<td id="C8">0</td>
<td id="C9">0</td>
</tr>
<tr>
<td>D</td>
<td id="D0">0</td>
<td id="D1">0</td>
<td id="D2">0</td>
<td id="D3">0</td>
<td id="D4">0</td>
<td id="D5">0</td>
<td id="D6">0</td>
<td id="D7">0</td>
<td id="D8">0</td>
<td id="D9">0</td>
</tr>
<tr>
<td>E</td>
<td id="E0">0</td>
<td id="E1">0</td>
<td id="E2">0</td>
<td id="E3">0</td>
<td id="E4">0</td>
<td id="E5">0</td>
<td id="E6">0</td>
<td id="E7">0</td>
<td id="E8">0</td>
<td id="E9">0</td>
</tr>
<tr>
<td>F</td>
<td id="F0">0</td>
<td id="F1">0</td>
<td id="F2">0</td>
<td id="F3">0</td>
<td id="F4">0</td>
<td id="F5">0</td>
<td id="F6">0</td>
<td id="F7">0</td>
<td id="F8">0</td>
<td id="F9">0</td>
</tr>
<tr>
<td>G</td>
<td id="G0">0</td>
<td id="G1">0</td>
<td id="G2">0</td>
<td id="G3">0</td>
<td id="G4">0</td>
<td id="G5">0</td>
<td id="G6">0</td>
<td id="G7">0</td>
<td id="G8">0</td>
<td id="G9">0</td>
</tr>
<tr>
<td>H</td>
<td id="H0">0</td>
<td id="H1">0</td>
<td id="H2">0</td>
<td id="H3">0</td>
<td id="H4">0</td>
<td id="H5">0</td>
<td id="H6">0</td>
<td id="H7">0</td>
<td id="H8">0</td>
<td id="H9">0</td>
</tr>
<tr>
<td>I</td>
<td id="I0">0</td>
<td id="I1">0</td>
<td id="I2">0</td>
<td id="I3">0</td>
<td id="I4">0</td>
<td id="I5">0</td>
<td id="I6">0</td>
<td id="I7">0</td>
<td id="I8">0</td>
<td id="I9">0</td>
</tr>
<tr>
<td>J</td>
<td id="J0">0</td>
<td id="J1">0</td>
<td id="J2">0</td>
<td id="J3">0</td>
<td id="J4">0</td>
<td id="J5">0</td>
<td id="J6">0</td>
<td id="J7">0</td>
<td id="J8">0</td>
<td id="J9">0</td>
</tr>
</tbody>
</table>
</body>
</html> |