support php8019 and some cleanup (by @D-aHTuCT)

This commit is contained in:
kotyara 2022-07-26 16:06:34 +05:00
parent cf0f9df267
commit b60f0877e1
376 changed files with 6528 additions and 13472 deletions

View file

@ -1,37 +1,19 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
//Unsecure, not used for "Real Randomness"
class Random{
private $x, $y, $z, $w;
private $haveNextNextGaussian = false;
private $nextNextGaussian = 0;
public function __construct($seed = false){
$this->setSeed($seed);
}
public function setSeed($seed = false){
$seed = $seed !== false ? Utils::writeInt((int) $seed):Utils::getRandomBytes(4, false);
$state = array();
$seed = $seed !== false ? Utils::writeInt((int) $seed) : Utils::getRandomBytes(4, false);
$state = [];
for($i = 0; $i < 256; ++$i){
$state[] = $i;
}
@ -44,39 +26,33 @@ class Random{
$this->state = $state;
$this->i = $this->j = 0;
}
public function nextGaussian(){
if ($this->haveNextNextGaussian) {
$this->haveNextNextGaussian = false;
if($this->haveNextNextGaussian){
$this->haveNextNextGaussian = false;
return $this->nextNextGaussian;
} else {
$v1 = $v2 = $s = null;
do {
$v1 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0
$v2 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0
$s = $v1 * $v1 + $v2 * $v2;
} while ($s >= 1 || $s == 0);
$multiplier = sqrt(-2 * log($s)/$s);
$nextNextGaussian = $v2 * $multiplier;
$haveNextNextGaussian = true;
return $v1 * $multiplier;
}
}
public function nextInt(){
return Utils::readInt($this->nextBytes(4)) & 0x7FFFFFFF;
}else{
$v1 = $v2 = $s = null;
do{
$v1 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0
$v2 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0
$s = $v1 * $v1 + $v2 * $v2;
}while($s >= 1 || $s == 0);
$multiplier = sqrt(-2 * log($s) / $s);
$nextNextGaussian = $v2 * $multiplier;
$haveNextNextGaussian = true;
return $v1 * $multiplier;
}
}
public function nextSignedInt(){
return Utils::readInt($this->nextBytes(4));
}
public function nextFloat(){
return $this->nextInt() / 0x7FFFFFFF;
}
public function nextSignedFloat(){
return $this->nextSignedInt() / 0x7FFFFFFF;
public function nextInt(){
return Utils::readInt($this->nextBytes(4)) & 0x7FFFFFFF;
}
public function nextBytes($byteCount){
$bytes = "";
for($i = 0; $i < $byteCount; ++$i){
@ -89,11 +65,19 @@ class Random{
}
return $bytes;
}
public function nextSignedFloat(){
return $this->nextSignedInt() / 0x7FFFFFFF;
}
public function nextSignedInt(){
return Utils::readInt($this->nextBytes(4));
}
public function nextBoolean(){
return ($this->nextBytes(1) & 0x01) == 0;
}
public function nextRange($start = 0, $end = PHP_INT_MAX){
return $start + ($this->nextInt() % ($end + 1 - $start));
}