Ver Mensaje Individual
  #9 (permalink)  
Antiguo 12/01/2012, 22:42
Avatar de HackmanC
HackmanC
 
Fecha de Ingreso: enero-2008
Ubicación: Guatemala
Mensajes: 1.817
Antigüedad: 17 años
Puntos: 260
Sonrisa Respuesta: Random() con posibilidad

Hola,

Código PHP:
Ver original
  1. $weights = array(
  2.      2 => 10,
  3.      4 => 10,
  4.     12 => 10,
  5.     24 => 30,
  6. );
  7. $totalWeight = array_sum($weights);
  8.  
  9. function normalize($n) {
  10.     global $weights;
  11.     $s = 0;
  12.     foreach ($weights as $key => $value) {
  13.         $s += $value;
  14.         if ($s >= $n) return $key;
  15.     }
  16.     return false;
  17. }
  18.  
  19. function randomizer() {
  20.     global $totalWeight;
  21.     $i = floor(mt_rand(0, $totalWeight));
  22.     return normalize($i);
  23. }
  24.  
  25. echo "select * from objects where id = " . randomizer();

En $weights tienes un array asociativo con el peso de cada id.

Saludos,

ps:
http://www.forosdelweb.com/f13/bajar...ricion-750353/