Ivan - Fix linear congruential generator

This commit is contained in:
ivan 2017-10-31 18:31:14 -03:00
parent 84b128326c
commit aa533856d5
4 changed files with 21 additions and 19 deletions

View File

@ -30,8 +30,8 @@ class Hashing {
$sqrt = sqrt($number);
$prime = true;
for($i = 0; $i < $sqrt; $i++) {
if($sqrt % 2 === 0) {
for($i = 2; $i <= $sqrt; $i++) {
if($number % $i === 0) {
$prime = false;
break;
}

View File

@ -21,8 +21,10 @@ class LinearCongruentialGenerator {
}
public function generate($offset) {
if($offset) return ($this->first - $this->min + $offset * $this->gap) % ($this->max - $this->min + 1) + $this->min;
else return $this->generateFirst();
if(!$this->first) throw new Exception('LinearCongruentialGenerator: first is not set');
if(!$this->gap) throw new Exception('LinearCongruentialGenerator: gap is not set');
return ($this->first - $this->min + $offset * $this->gap) % ($this->max - $this->min + 1) + $this->min;
}
public function generateFirst() {