unicRandomizer class php
<?php
/* unicRandomizer
construction $=new unicRandomizer(min, max, resetLimit)
parameters :
min : int, def 0, minimal random interval
max : int, def 100, maximal random interval
resetLimit : int, def 1000, cached values limit
reset()
reset cache
no parameters, returns the number of suppressed values
setMin(min,reset)
set minimal value of random interval
parameters :
min : int, def 0
reset : bool, def true
return :
0 setting ok, 1 setting corrected and ok, -1 error
setMax(max,reset)
set maximal value of random interval
parameters :
max : int, def 0
reset : bool, def true
return :
0 setting ok, 1 setting corrected and ok, -1 error
setMinMax(min,max,reset)
set minimal and maximal values of random interval
parameters :
min : int, def 0
max : int, def 100
reset : bool, def true
return :
array("min","max") values : 0 setting ok, 1 setting corrected and ok, -1 error
rand()
generate unique random value
no parameters, return random value in min>=value<=max interval
*/
class unicRandomizer{
protected $cache=array();
protected $resetLimit=1000;
protected $min=0;
protected $max=100;
public function __construct($min=0,$max=100,$resetLimit=1000){
$this->cache=array();
$this->resetLimit=$resetLimit;
$this->min=$min;
$this->max=$max;
rand(0,100);
}
public function reset(){
$ret=count($this->cache);
$this->cache=array();
return $ret;
}
public function setMin($min=0,$reset=true){
if($min > $this->max){
$this->min=$this->max;
$this->max=$min;
if($reset)$this->reset();
return 1;
} else {
$this->min=$min;
if($reset)$this->reset();
return 0;
}
return -1;
}
public function setMax($max=100,$reset=true){
if($max < $this->min){
$this->max=$this->min;
$this->min=$max;
if($reset)$this->reset();
return 1;
} else {
$this->max=$max;
if($reset)$this->reset();
return 0;
}
return -1;
}
public function setMinMax($min=0,$max=100,$reset=true){
$ret=array(
"min"=>$this->setMin($min,false),
"max"=>$this->setMax($max,$reset)
);
return $ret;
}
public function rand(){
$ret=rand($this->min, $this->max);
while(in_array($ret, $this->cache)){
$ret=rand($this->min, $this->max);
}
array_push($this->cache,$ret);
if(count($this->cache) >= $this->resetLimit){
$this->reset();
}
return $ret;
}
}
?>
unicRandomizer demo php
<?php
include_once("unicRandomizer.class.php");
$ur=new unicRandomizer(100000,999999);
?>
<html>
<head>
</head>
<?php
for($i=0;$i<100;$i++){
echo $ur->rand(),'
';
}
?>
</html>
________________________________________________________