Ivan - Fix linear congruential generator
This commit is contained in:
parent
84b128326c
commit
aa533856d5
|
@ -93,7 +93,7 @@ class InitSettingsController extends Controller {
|
|||
|
||||
private function storeMailTemplates() {
|
||||
$mails = InitialMails::retrieve();
|
||||
|
||||
|
||||
foreach ($mails as $mailType => $mailLanguages) {
|
||||
foreach ($mailLanguages as $mailLanguage => $mailContent) {
|
||||
$mailTemplate = new MailTemplate();
|
||||
|
@ -124,7 +124,7 @@ class InitSettingsController extends Controller {
|
|||
}
|
||||
private function storeLanguages() {
|
||||
$defaultLanguage = Controller::request('language');
|
||||
|
||||
|
||||
foreach(Language::LANGUAGES as $languageCode) {
|
||||
$language = new Language();
|
||||
$language->setProperties([
|
||||
|
|
|
@ -27,10 +27,10 @@ abstract class Controller {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public function validate() {
|
||||
$validator = new Validator();
|
||||
|
||||
|
||||
$validator->validate($this->validations());
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ abstract class Controller {
|
|||
|
||||
public static function request($key, $secure = false) {
|
||||
$result = call_user_func(self::$dataRequester, $key);
|
||||
|
||||
|
||||
if($secure) {
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$purifier = new HTMLPurifier($config);
|
||||
|
@ -63,7 +63,7 @@ abstract class Controller {
|
|||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static function getLoggedUser() {
|
||||
$session = Session::getInstance();
|
||||
|
||||
|
@ -90,7 +90,7 @@ abstract class Controller {
|
|||
public static function getAppInstance() {
|
||||
return \Slim\Slim::getInstance();
|
||||
}
|
||||
|
||||
|
||||
public function uploadFile($forceUpload = false) {
|
||||
$allowAttachments = Setting::getSetting('allow-attachments')->getValue();
|
||||
|
||||
|
@ -114,8 +114,8 @@ abstract class Controller {
|
|||
throw new Exception(ERRORS::INVALID_FILE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static function isUserSystemEnabled() {
|
||||
return Setting::getSetting('user-system-enabled')->getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
@ -39,4 +39,4 @@ class Hashing {
|
|||
|
||||
return $prime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ class LinearCongruentialGenerator {
|
|||
private $first;
|
||||
private $min = 100000;
|
||||
private $max = 999999;
|
||||
|
||||
|
||||
public function setRange($min, $max) {
|
||||
$this->min = $min;
|
||||
$this->max = $max;
|
||||
|
@ -12,20 +12,22 @@ class LinearCongruentialGenerator {
|
|||
|
||||
public function setGap($gap) {
|
||||
if(!Hashing::isPrime($gap)) throw new Exception('LinearCongruentialGenerator: gap must be prime');
|
||||
|
||||
|
||||
$this->gap = $gap;
|
||||
}
|
||||
|
||||
public function setFirst($first) {
|
||||
$this->first = $first;
|
||||
}
|
||||
|
||||
|
||||
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() {
|
||||
return Hashing::generateRandomNumber($this->min, $this->max);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue