_parameters = new Zformat\Parameter( __CLASS__, [], [ 'integer.min' => -16, 'integer.max' => 15, 'float.min' => -128.0, 'float.max' => 127.0 ] ); $this->_parameters->setParameters($parameters); if (null === $this->_parameters->getParameter('integer.min')) { $this->_parameters->setParameter('integer.min', PHP_INT_MIN); } if (null === $this->_parameters->getParameter('integer.max')) { $this->_parameters->setParameter('integer.max', PHP_INT_MAX); } if (null === $this->_parameters->getParameter('float.min')) { $this->_parameters->setParameter('float.min', PHP_INT_MIN); } if (null === $this->_parameters->getParameter('float.max')) { $this->_parameters->setParameter('float.max', PHP_INT_MAX); } $this->construct(); return; } /** * Construct. * * @return void */ public function construct() { return; } /** * Get parameters. * * @return \Hoa\Zformat\Parameter */ public function getParameters() { return $this->_parameters; } /** * Generate a discrete uniform distribution. * * @param int $lower Lower bound value. * @param int $upper Upper bound value. * @param array &$exclude Excluded values. * @return int */ public function getInteger( $lower = null, $upper = null, array &$exclude = null ) { if (null === $lower) { $lower = $this->_parameters->getParameter('integer.min'); } if (null === $upper) { $upper = $this->_parameters->getParameter('integer.max'); } if (null === $exclude) { if ($lower > $upper) { throw new Math\Exception( 'Unexpected values, integer %d should be lower than %d', 0, [$lower, $upper] ); } return $this->_getInteger($lower, $upper); } sort($exclude); $upper -= count($exclude); if ($lower > $upper) { throw new Math\Exception( 'Unexpected values, integer %d should be lower than %d', 1, [$lower, $upper] ); } $sampled = $this->_getInteger($lower, $upper); foreach ($exclude as $e) { if ($sampled >= $e) { ++$sampled; } else { break; } } return $sampled; } /** * Generate a discrete uniform distribution. * * @param int $lower Lower bound value. * @param int $upper Upper bound value. * @return int */ abstract protected function _getInteger($lower, $upper); /** * Generate a continuous uniform distribution. * * @param float $lower Lower bound value. * @param float $upper Upper bound value. * @return float */ public function getFloat($lower = null, $upper = null) { if (null === $lower) { $lower = $this->_parameters->getParameter('float.min'); } /* $lower = true === S_32\BITS ? -3.4028235e38 + 1 : -1.7976931348623157e308 + 1; */ if (null === $upper) { $upper = $this->_parameters->getParameter('float.max'); } /* $upper = true === S_32\BITS ? 3.4028235e38 - 1 : 1.7976931348623157e308 - 1; */ if ($lower > $upper) { throw new Math\Exception( 'Unexpected values, float %f should be lower than %f', 2, [$lower, $upper]); } return $this->_getFloat($lower, $upper); } /** * Generate a continuous uniform distribution. * * @param float $lower Lower bound value. * @param float $upper Upper bound value. * @return float */ abstract protected function _getFloat($lower, $upper); /** * Get an exclude set. * * @return array */ public function getExcludeSet() { return []; } } /** * Flex entity. */ Consistency::flexEntity('Hoa\Math\Sampler\Sampler');