Ver Mensaje Individual
  #4 (permalink)  
Antiguo 16/08/2011, 22:30
Avatar de portalmana
portalmana
 
Fecha de Ingreso: septiembre-2007
Ubicación: Montevideo-Uruguay
Mensajes: 633
Antigüedad: 16 años, 7 meses
Puntos: 80
Respuesta: Generador de numeros aleatorios

Bueno esto no es exactamente pseudo aleatorio pero se parece...

Código PHP:
Ver original
  1. <?php
  2. class Aleatorio2
  3. {
  4.     /**
  5.      * Número de microsegundos que han transcurrido desde $_tiempoUnix
  6.      * expresado en segundos.
  7.      * @since Muy Variable
  8.      * @var string
  9.      */
  10.     private $_microSegundos;
  11.    
  12.     /**
  13.      * Momento actual medido con el número de segundos desde la época Unix
  14.      * (0:00:00 January 1, 1970 GMT)
  15.      * @since Variable
  16.      * @var string
  17.      */
  18.     private $_tiempoUnix;
  19.    
  20.     /**
  21.      * Cantidad de memoria asignada a PHP en bytes.
  22.      * @since muy Fija
  23.      * @var int
  24.      */
  25.     private $_memAsignadaPHP;
  26.    
  27.     /**
  28.      * Cantidad de memoria en uso por PHP en bytes.
  29.      * @since Variable
  30.      * @var int
  31.      */
  32.     private $_memUsadaPHP;
  33.    
  34.     /**
  35.      * Cantidad de memoria usada por el script en ejecucion.
  36.      * @since poco Variable
  37.      * @var int
  38.      */
  39.     private $_memUsadaScript;
  40.    
  41.     /**
  42.      * Espacio libre en disco.
  43.      * @since variable
  44.      * @var int
  45.      */
  46.     private $_libreEnDisco;
  47.    
  48.     /**
  49.      * Espacio total del disco.
  50.      * @since fijo
  51.      * @var int
  52.      */
  53.     private $_tamanioDisco;
  54.    
  55.     /**
  56.      * Valor de hash sha1.
  57.      * @var string
  58.      */
  59.     private $_sha1;
  60.    
  61.     /**
  62.      * Maximo numero aleatorio generado por la clase.
  63.      * @var float
  64.      */
  65.     private $_maxAleatorio;
  66.    
  67.     /**
  68.      * Inicializa la clase.
  69.      * Especifica el maximo valor aleatorio que se puede generar.
  70.      * @return void
  71.      */
  72.     public function __construct()
  73.     {
  74.         $this->_maxAleatorio    = hexdec('FFFFFFFFFFF');
  75.     }
  76.    
  77.     /**
  78.      * Genera un numero aleatorio entre 0 y 1.
  79.      * @return float
  80.      */
  81.     public function aleatorio()
  82.     {
  83.         $this->_recalcular();
  84.         $multiplicador          = $this->_microSegundos * $this->_tiempoUnix;
  85.         $principio              = substr($multiplicador, strpos($multiplicador,'.')-1, 1);
  86.         $dividendo              = hexdec(substr($this->_sha1,$principio,11));
  87.         $aleatorio              = $dividendo / $this->_maxAleatorio;
  88.         return $aleatorio;
  89.     }
  90.    
  91.    
  92.     /**
  93.      * Recalcula los valores que se toman para calcular el numero pseudo-
  94.      * aleatorio.
  95.      * @return void
  96.      */
  97.     private function _recalcular()
  98.     {
  99.         $datosVariables         = explode(' ',microtime()) ;
  100.         $this->_microSegundos   = $datosVariables[0];
  101.         $this->_tiempoUnix      = $datosVariables[1];
  102.         $this->_memAsignadaPHP  = memory_get_peak_usage(true);
  103.         $this->_memUsadaPHP     = memory_get_peak_usage(false);
  104.         $this->_memUsadaScript  = memory_get_usage ();
  105.         $this->_tamanioDisco    = disk_total_space('/');
  106.         $this->_libreEnDisco    = disk_free_space('/');
  107.         $sha1                   = $this->_microSegundos
  108.                                 . $this->_tiempoUnix
  109.                                 . $this->_memAsignadaPHP
  110.                                 . $this->_memUsadaPHP
  111.                                 . $this->_memUsadaScript
  112.                                 . $this->_tamanioDisco
  113.                                 . $this->_libreEnDisco;
  114.         $this->_sha1            = sha1($sha1);
  115.     }
  116. }
  117.  
  118. // Ejemplo
  119. $arreglo    = array();
  120. $obj = new Aleatorio2();
  121.  
  122. $min = 1;
  123. $max = 10;
  124.  
  125. for($i=1; $i<10000; $i++) {
  126.     $nro = $obj->aleatorio();
  127.     $aleat  =  $min + intval(($max - $min +1 )* $nro);
  128.     $arreglo[$aleat]++;
  129. }
  130.  
  131. ksort($arreglo);
  132. echo '<pre>';
  133. var_dump($arreglo);
  134. echo '</pre>';

Lo único que tiene es Sha1 pero no se usa para aleatorizar cosas solo es para obtener hash.
__________________
"La imaginación es más importante que el conocimiento. El conocimiento es limitado, mientras que la imaginación no" -- A.Einstein
objetivophp.com,twitter.com/objetivophp