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); $sqrt = sqrt($number);
$prime = true; $prime = true;
for($i = 0; $i < $sqrt; $i++) { for($i = 2; $i <= $sqrt; $i++) {
if($sqrt % 2 === 0) { if($number % $i === 0) {
$prime = false; $prime = false;
break; break;
} }

View File

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