Skip to content

Commit 73ef997

Browse files
author
MarkBaker
committed
Finishing off the last of the Math/Trig functions, beginning with RANDBETWEEN()
1 parent de2fb1f commit 73ef997

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/PhpSpreadsheet/Calculation/MathTrig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ public static function QUOTIENT($numerator, $denominator)
597597
* @param int $min Minimal value
598598
* @param int $max Maximal value
599599
*
600-
* @return float|int|string Random number
600+
* @return array|float|int|string Random number
601601
*/
602602
public static function RAND($min = 0, $max = 0)
603603
{

src/PhpSpreadsheet/Calculation/MathTrig/Random.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78

89
class Random
910
{
11+
use ArrayEnabled;
12+
1013
/**
1114
* RAND.
1215
*
@@ -21,12 +24,20 @@ public static function rand()
2124
* RANDBETWEEN.
2225
*
2326
* @param mixed $min Minimal value
27+
* Or can be an array of values
2428
* @param mixed $max Maximal value
29+
* Or can be an array of values
2530
*
26-
* @return float|int|string Random number
31+
* @return array|float|int|string Random number
32+
* If an array of numbers is passed as an argument, then the returned result will also be an array
33+
* with the same dimensions
2734
*/
2835
public static function randBetween($min, $max)
2936
{
37+
if (is_array($min) || is_array($max)) {
38+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $min, $max);
39+
}
40+
3041
try {
3142
$min = (int) Helpers::validateNumericNullBool($min);
3243
$max = (int) Helpers::validateNumericNullBool($max);

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/RandBetweenTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
57
class RandBetweenTest extends AllSetupTeardown
68
{
79
/**
@@ -43,4 +45,30 @@ public function providerRANDBETWEEN(): array
4345
{
4446
return require 'tests/data/Calculation/MathTrig/RANDBETWEEN.php';
4547
}
48+
49+
/**
50+
* @dataProvider providerRandBetweenArray
51+
*/
52+
public function testRandBetweenArray(
53+
int $expectedRows,
54+
int $expectedColumns,
55+
string $argument1,
56+
string $argument2
57+
): void {
58+
$calculation = Calculation::getInstance();
59+
60+
$formula = "=RandBetween({$argument1}, {$argument2})";
61+
$result = $calculation->_calculateFormulaValue($formula);
62+
self::assertIsArray($result);
63+
self::assertCount($expectedRows, $result);
64+
self::assertIsArray($result[0]);
65+
self::assertCount($expectedColumns, $result[0]);
66+
}
67+
68+
public function providerRandBetweenArray(): array
69+
{
70+
return [
71+
'row/column vectors' => [2, 2, '{1, 10}', '{10; 100}'],
72+
];
73+
}
4674
}

0 commit comments

Comments
 (0)