/** * calculates the offset based on the wrap flag * * This is generally used by array functions to wrap offsets * * @param Integer $length Starting from 1 (not 0), the length of the list being wrapped around * @param Integer $offset The offset being wrapped * @param Integer $wrapFlag How to handle offsets that fall outside of the length of the list. * @return Integer|Boolean Returns the wrapped offset. Returns FALSE on failure */ function offsetWrap($length, $offset, $wrapFlag) { $length = (int) $length; if ($length <= 0) { throw new \r8\Exception\Index($offset, "Offset", "List is empty"); } $offset = (int) \r8\reduce($offset); switch ($wrapFlag) { default: throw new \r8\Exception\Argument(2, "wrapFlag", "Invalid offset wrap flag"); case \r8\num\OFFSET_NONE: if (!\r8\num\between($offset, 0 - $length, $length - 1)) { throw new \r8\Exception\Index($offset, "Offset", "Offset is out of bounds"); } else { if ($offset >= 0) { return $offset; } else { return $length + $offset; } } case \r8\num\OFFSET_WRAP: return \r8\num\intWrap($offset, 0, $length - 1); case FALSE: case \r8\num\OFFSET_RESTRICT: $offset = \r8\num\limit($offset, 0 - $length, $length - 1); if ($offset < 0) { $offset = $length + $offset; } return $offset; case \r8\num\OFFSET_LIMIT: return \r8\num\limit($offset, 0, $length - 1); } }
/** * Returns the next random number as an integer between the given minimum * and maximum * * @param Integer $min The minimum allowed value, inclusive * @param Integer $max The maximum value, inclusive * @return Integer */ public function nextRange($min, $max) { return \r8\num\intWrap($this->nextInteger(), $min, $max); }
function testIntWrap() { $this->assertEquals(15, \r8\num\intWrap(37, 10, 20)); $this->assertEquals(15, \r8\num\intWrap(26, 10, 20)); $this->assertEquals(15, \r8\num\intWrap(15, 10, 20)); $this->assertEquals(15, \r8\num\intWrap(4, 10, 20)); $this->assertEquals(15, \r8\num\intWrap(-7, 10, 20)); $this->assertEquals(10, \r8\num\intWrap(-1, 10, 20)); $this->assertEquals(10, \r8\num\intWrap(10, 10, 20)); $this->assertEquals(20, \r8\num\intWrap(20, 10, 20)); $this->assertEquals(20, \r8\num\intWrap(31, 10, 20)); }