Ver Mensaje Individual
  #3 (permalink)  
Antiguo 02/09/2010, 15:57
Avatar de Xerelo
Xerelo
 
Fecha de Ingreso: mayo-2009
Mensajes: 2.175
Antigüedad: 15 años
Puntos: 306
Respuesta: entero aleatorio distribucion uniforme

Hay un algoritmo muy eficiente para eso.

Código Javascript:
Ver original
  1. int a[] = {1,2,3,4,5}
  2. int ind;
  3. int aux;
  4. Random r = new Random();
  5.  
  6. for (int i=4;i>-1;i--){
  7.  
  8. ind = r.nextInt(i+1);
  9.  
  10. aux = a[i];
  11. a[i] = a[ind];
  12. a[ind] = aux;
  13.  
  14. System.out.println(a[i]);
  15.  
  16. }

Por si no se ve claro (o lo he escrito mal), se trata de elegir una posición aleatoria x entre n valores, intercambiar valores entre x y n, repetir aleatorio con n-1 valores intercambiando x y n-1, y así sucesivamente.