This thread is inspired by this thread
http://www.phpbuilder.com/board/showthread.php?s=&threadid=10242753
<?php
// by : Jimson Chang
define('RSIZE', 7); // size of random number
define('LOOPCOUNT', 70000); // count of generated random number
define('SAMPELS', 40); // samples of LOOPCOUNT generated random number
define('BPOINT', LOOPCOUNT / RSIZE); // middle point that break number into less than and more than
define('PERCENT', 0.05); // percentage used for +-
define('PLUSMINUS', PERCENT * BPOINT); // plus minus BPOINT value that considered it to be in the middle
// change this value according to your LOOPCOUNT value
define('LOWBPOINT', BPOINT-PLUSMINUS);
define('HIGHBPOINT',BPOINT+PLUSMINUS);
function random_generate($a=null) {
return rand(1, RSIZE);
}
function random_stats($rKey) {
static $data;
switch($rKey) {
case 'g':
return $data;
break;
case 'reset':
$data = null;
break;
default:
if(!isset($data[$rKey])) {
$data[$rKey] = 1;
} else {
$data[$rKey]++;
}
}
}
function random_finalize_sampels($rKey = NULL) {
static $dataA;
static $holder;
static $currentVal;
static $currentKey;
static $i = 1;
switch($rKey) {
case 'g':
return $dataA;
break;
case 'store':
if(!isset($dataA[$i]['below'])) $dataA[$i]['below'] = 0;
if(!isset($dataA[$i]['equal'])) $dataA[$i]['equal'] = 0;
if(!isset($dataA[$i]['larger'])) $dataA[$i]['larger'] = 0;
$dataA[$i][$holder]++;
break;
case 'lowhigh':
if(!isset($dataA[$i]['low'])) {
$dataA[$i]['low'] = array('k' => $currentKey, 'v' => $currentVal);
}
if(!isset($dataA[$i]['high'])) {
$dataA[$i]['high'] = array('k' => $currentKey, 'v' => $currentVal);
break;
}
if($currentVal > $dataA[$i]['high']['v']) {
$dataA[$i]['high'] = array('k' => $currentKey, 'v' => $currentVal);
}
if($currentVal < $dataA[$i]['low']['v']) {
$dataA[$i]['low'] = array('k' => $currentKey, 'v' => $currentVal);
}
break;
case 'decide':
$temp = random_stats('g');
for($j=1; $j<=RSIZE; $j++) {
$currentVal = $temp[$j];
$currentKey = $j;
if(($temp[$j] > LOWBPOINT) AND ($temp[$j] < HIGHBPOINT)) {
$holder = 'equal';
} elseif($temp[$j] < LOWBPOINT) {
$holder = 'below';
} else {
$holder = 'larger';
}
random_finalize_sampels('store');
random_finalize_sampels('lowhigh');
}
$i++;
break;
default:
random_finalize_sampels('decide');
random_stats('reset');
}
}
function random_highlow(&$rData) {
$rHigh = max($rData);
$rLow = min($rData);
$rData = array_flip($rData);
$r['high'] = array($rHigh, $rData[$rHigh]);
$r['low'] = array($rLow, $rData[$rLow]);
return $r;
}
// +------+
// | main |
// +------+
for($i=1; $i<=SAMPELS; $i++) {
for($j=1; $j<=LOOPCOUNT; $j++) {
random_stats(random_generate());
}
random_finalize_sampels();
}
?>
<html>
<title>random number</title>
<body bgcolor=#DFDFDF>
<b>Test Environment</b>
<table border=1 cellpadding=5 cellspacing=0>
<tr><td>random number size</td><td><?php echo RSIZE?></td></tr>
<tr><td>loop count</td><td><?php echo LOOPCOUNT?></td></tr>
<tr><td>total sampels</td><td><?php echo SAMPELS?></td></tr>
<tr><td>break point (actual) occurance in each sampels</td><td><?php echo BPOINT?></td></tr>
<tr><td>low break point occurance in each sampels</td><td><?php echo LOWBPOINT?></td></tr>
<tr><td>high break point occurance in each sampels</td><td><?php echo HIGHBPOINT?></td></tr>
</table><br>
<b>Sampel's Analysis</b><br>
HOGRN = Highest occurance generated random number ( num => occurance )<br>
LOGRN = Lowest occurance generated random number ( num => occurance )<br>
TOINS = Total occurance in sampel for <?php echo RSIZE?> numbers.<br>
<table border=1 cellpadding=5 cellspacing=0>
<tr align=center bgcolor=#888888>
<td>Sampel Num</td>
<td>HOGRN</td>
<td>LOGRN</td>
<td>HOGRN - LOGRN</td>
<td>TOINS < <?php echo LOWBPOINT?></td>
<td><?php echo LOWBPOINT.' < TOINC < '.HIGHBPOINT?></td>
<td>TOINS > <?php echo HIGHBPOINT?></td>
</tr>
<?php
static $total = array('below' => 0, 'equal' => 0, 'larger' => 0);
static $low;
static $high;
$dataA = random_finalize_sampels('g');
for($i=1; $i<=sizeof($dataA); $i++) {
$highKey = $dataA[$i]['high']['k'];
$lowKey = $dataA[$i]['low']['k'];
$highVal = $dataA[$i]['high']['v'];
$lowVal = $dataA[$i]['low']['v'];
$diff = $highVal-$lowVal;
echo "<tr align=center>".
"<td>$i</td>".
"<td>$highKey => $highVal</td>".
"<td>$lowKey => $lowVal</td>".
"<td>$diff</td>".
"<td>{$dataA[$i]['below']}</td>".
"<td>{$dataA[$i]['equal']}</td>".
"<td>{$dataA[$i]['larger']}</td>".
"</tr>";
$total['below'] += $dataA[$i]['below'];
$total['equal'] += $dataA[$i]['equal'];
$total['larger'] += $dataA[$i]['larger'];
if(!isset($high[$highKey])) $high[$highKey] = 0;
if(!isset($low[$lowKey])) $low[$lowKey] = 0;
$high[$highKey]++;
$low[$lowKey]++;
}
$high = random_highlow($high);
$low = random_highlow($low);
?>
<tr align=center bgcolor=#888888>
<td> </td>
<td>
high: <?php echo $high['high'][1].' => '.$high['high'][0].' occ' ?><br>
low: <?php echo $high['low'][1].' =>'.$high['low'][0].' occ' ?><br>
</td>
<td>
high: <?php echo $low['high'][1].' => '.$low['high'][0].' occ' ?><br>
low: <?php echo $low['low'][1].' => '.$low['low'][0].' occ' ?><br>
</td>
<td> </td>
<td><?php echo $total['below']?></td>
<td><?php echo $total['equal']?></td>
<td><?php echo $total['larger']?></td>
</tr>
</table>
<br>
<b>Summary</b><br>
<?php
echo 'Total generated random numbers = '.SAMPELS*LOOPCOUNT.'<br><br>'.
'<b>NOT RANDOM</b><br>'.
'When not random, '.SAMPELS*LOOPCOUNT.' divide by '.RSIZE.' = '.(SAMPELS*LOOPCOUNT)/RSIZE.' numbers should occures in this value.<br>
Which means also each sampel should have '.SAMPELS*LOOPCOUNT.' divide by '.SAMPELS.' => '.(SAMPELS*LOOPCOUNT)/SAMPELS.' divide by '.RSIZE.' = '.((SAMPELS*LOOPCOUNT)/SAMPELS)/RSIZE.'
values which are <b>SAME</b>.<br>'.
'In not random way, each sampel should will have only 1 state which is equal, or <b>TOINS</b> = '.LOOPCOUNT/RSIZE.'<br>
where in the total row ('.SAMPELS.' sampels), it should yield '.SAMPELS*RSIZE.' in ( TOINS = '.LOOPCOUNT/RSIZE.' ) column only. (becoz there is only one state)<br>'.
'<br><br>'.
'<b>WHEN WE RANDOM IT, THE RESULT SHOULD NOT BE THE SAME LIKE NOT RANDOM</b><br>'.
'We plus minus '.PERCENT * 100 .' % to make the break point scalable a byte.<br>
And now, we have 3 states, where < TOINS or > TOINS or equal to (+-)TOINS.<br>
When you got (+-)'.SAMPELS*RSIZE.' in (+-)TOINS column, it is nearly <b>NOT RANDOM</b> in my point of view.';
?>
</body>
</html>