updated prng

This commit is contained in:
Steffen Vogel 2010-09-22 02:27:15 +02:00
parent 11ec19d486
commit d1be7dc3fc
2 changed files with 16 additions and 8 deletions

View file

@ -30,7 +30,7 @@ namespace Volkszaehler\Util;
* @package util
*/
class Random {
protected static $func = 'twister';
protected static $func = NULL;
protected static $source = NULL;
/**
@ -44,22 +44,29 @@ class Random {
self::$source = fopen('/dev/urandom', 'rb');
self::$func = 'fRead';
}
else if (class_exists('COM', 0)) {
elseif (class_exists('COM', 0)) {
try {
self::$source = new COM('CAPICOM.Utilities.1'); // See http://msdn.microsoft.com/en-us/library/aa388182(VS.85).aspx
self::$func = 'COM';
}
catch(\Exception $e) {}
}
else {
self::$func = 'twister';
}
return self::$func;
}
/**
*
* @param intger $bytes
* @todo check if initialized
*/
public static function getBytes($count) {
if (!isset(self::$func)) {
self::init();
}
return call_user_func(array('self', self::$func), $count);
}
@ -69,7 +76,7 @@ class Random {
* @param integer $count length of string
*/
public function getString(array $chars, $length) {
$numbers = self::get(0, count($chars) - 1, $length);
$numbers = self::getNumbers(0, count($chars) - 1, $length);
$string = '';
foreach ($numbers as $number) {
@ -87,7 +94,7 @@ class Random {
* @param integer $count
* @return integer|array single integer if $count == 1 or array of integers if $count > 1
*/
public function get($min, $max, $count = 1) {
public static function getNumbers($min, $max, $count = 1) {
$bytes = self::getBytes($count);
$numbers = array();

View file

@ -31,8 +31,6 @@ include '../../backend/lib/Model/Token.php';
use Volkszaehler\Model;
use Volkszaehler\Util;
Util\Random::init();
$chars = array(
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'v', 'u', 'w', 'x', 'y', 'z',
@ -41,10 +39,13 @@ $chars = array(
?>
<h4>PRNG tests</h4>
<p>PNRG generator: <?= Util\Random::init() ?></p>
<pre>
<?php
echo 'Numbers: ' . implode(',', Util\Random::get(0, 100, 100)) . PHP_EOL;
echo 'Numbers: ' . implode(',', Util\Random::getNumbers(0, 15, 10)) . PHP_EOL;
echo 'String: ' . Util\Random::getString($chars, 100) . PHP_EOL;
echo 'Bytes: ' . Util\Random::getBytes(100) . PHP_EOL;
?>