Merge pull request #373 from terrafrost/another-random-change

BigInteger: make it so you can do $min->random($max)

* terrafrost/another-random-change:
  BigInteger: one more change to the comments
  BigInteger: update comments
  BigInteger: unit test update
  BigInteger: change argument names for random / randomPrime
  BigInteger: fix unit tests
  BigInteger: fix syntax error
  BigInteger: make it so you can do $min->random($max)
This commit is contained in:
Andreas Fischer 2014-06-16 16:54:00 +02:00
commit 088e31b6ea
2 changed files with 60 additions and 16 deletions

View File

@ -3088,19 +3088,31 @@ class Math_BigInteger
/** /**
* Generate a random number * Generate a random number
* *
* @param optional Integer $min * Returns a random number between $min and $max where $min and $max
* @param optional Integer $max * can be defined using one of the two methods:
*
* $min->random($max)
* $max->random($min)
*
* @param Math_BigInteger $arg1
* @param optional Math_BigInteger $arg2
* @return Math_BigInteger * @return Math_BigInteger
* @access public * @access public
* @internal The API for creating random numbers used to be $a->random($min, $max), where $a was a Math_BigInteger object.
* That method is still supported for BC purposes.
*/ */
function random($min = false, $max = false) function random($arg1, $arg2 = false)
{ {
if ($min === false) { if ($arg1 === false) {
$min = new Math_BigInteger(0); return false;
} }
if ($max === false) { if ($arg2 === false) {
$max = new Math_BigInteger(0x7FFFFFFF); $max = $arg1;
$min = $this;
} else {
$min = $arg1;
$max = $arg2;
} }
$compare = $max->compare($min); $compare = $max->compare($min);
@ -3163,21 +3175,25 @@ class Math_BigInteger
* If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed, * If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed,
* give up and return false. * give up and return false.
* *
* @param optional Integer $min * @param Math_BigInteger $arg1
* @param optional Integer $max * @param optional Math_BigInteger $arg2
* @param optional Integer $timeout * @param optional Integer $timeout
* @return Math_BigInteger * @return Mixed
* @access public * @access public
* @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}. * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}.
*/ */
function randomPrime($min = false, $max = false, $timeout = false) function randomPrime($arg1, $arg2 = false, $timeout = false)
{ {
if ($min === false) { if ($arg1 === false) {
$min = new Math_BigInteger(0); return false;
} }
if ($max === false) { if ($arg2 === false) {
$max = new Math_BigInteger(0x7FFFFFFF); $max = $arg1;
$min = $this;
} else {
$min = $arg1;
$max = $arg2;
} }
$compare = $max->compare($min); $compare = $max->compare($min);
@ -3289,7 +3305,7 @@ class Math_BigInteger
* $t parameter is distributability. Math_BigInteger::randomPrime() can be distributed across multiple pageloads * $t parameter is distributability. Math_BigInteger::randomPrime() can be distributed across multiple pageloads
* on a website instead of just one. * on a website instead of just one.
* *
* @param optional Integer $t * @param optional Math_BigInteger $t
* @return Boolean * @return Boolean
* @access public * @access public
* @internal Uses the * @internal Uses the

View File

@ -266,6 +266,34 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase
$this->assertSame('18446744073709551616', (string) $y); $this->assertSame('18446744073709551616', (string) $y);
} }
public function testRandomTwoArgument()
{
$min = $this->getInstance(0);
$max = $this->getInstance('18446744073709551616');
$rand1 = $min->random($min, $max);
// technically $rand1 can equal $min but with the $min and $max we've
// chosen it's just not that likely
$this->assertTrue($rand1->compare($min) > 0);
$this->assertTrue($rand1->compare($max) < 0);
}
public function testRandomOneArgument()
{
$min = $this->getInstance(0);
$max = $this->getInstance('18446744073709551616');
$rand1 = $min->random($max);
$this->assertTrue($rand1->compare($min) > 0);
$this->assertTrue($rand1->compare($max) < 0);
$rand2 = $max->random($min);
$this->assertTrue($rand2->compare($min) > 0);
$this->assertTrue($rand2->compare($max) < 0);
$this->assertFalse($rand1->equals($rand2));
}
/** /**
* @group github279 * @group github279
*/ */