Generador de arrays de numeros aleatorios sin repetir.
<form method="POST" action="AleatorioArray.php">
<table>
<tr bgcolor="#CCCCCC">
<td>Menor</td>
<td>Mayor</td>
</tr>
<tr>
<td><input type="text" size="4" name="menor" /></td>
<td><input type="text" size="4" name="mayor" /></td>
</tr>
<tr>
<td>Cantidad </td>
<td><input type="text" size="4" name="num" /></td>
</tr>
<tr>
<td colspan="2">Cantidad <input type="submit" size="4" name="envio" value="Calcular" /></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['menor'], $_POST['mayor'], $_POST['num'])){
$num = Array();
reset($num);
for($i=1;$i<=$_POST['num'];$i++)
{
$num[$i]=rand($_POST['menor'],$_POST['mayor']);
if($i>1)
{
for($x=1; $x<$i; $x++)
{
if($num[$i]==$num[$x])
{
$i--;
break;
}
}
}
}
foreach($num as $valor){
echo "$valor<BR>";
}
}
Mostrando entradas con la etiqueta vector. Mostrar todas las entradas
Mostrando entradas con la etiqueta vector. Mostrar todas las entradas
martes, 2 de agosto de 2011
jueves, 9 de junio de 2011
Vector con numeros aleatorios sin repetir JAVA
Codigo en Java, que llena un vector de 10 pocisiones aleatoriamente sin numeros repetidos. Si quieres un vector de mas Numeros solo cambia el valor de n = ?
int n = 10;
int k = n;
int[] resultado = new int[n];
int[] numeros=new int[n];
Random rnd = new Random();
int res;
for(int i=0;i<n;i++){
numeros[i]=i+1;
}
for(int i=0;i<n;i++){
res = rnd.nextInt(k);
resultado[i]=numeros[res];
numeros[res]=numeros[k-1];
k--;
}
for(int i = 0; i<10; i++){
System.out.println(resultado[i]);
}
Suscribirse a:
Entradas (Atom)