Failed Conditions
Pull Request — master (#3876)
by Abdul Malik
22:45 queued 13:31
created

Random   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 31
dl 0
loc 88
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
7
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8
9
class Random
10
{
11
    use ArrayEnabled;
12
13
    /**
14
     * RAND.
15
     *
16
     * @return float|int Random number
17
     */
18 4
    public static function rand(): int|float
19
    {
20 4
        return mt_rand(0, 10_000_000) / 10_000_000;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting ',' or ')' on line 20 at column 28
Loading history...
21
    }
22
23
    /**
24
     * RANDBETWEEN.
25
     *
26
     * @param mixed $min Minimal value
27
     *                      Or can be an array of values
28
     * @param mixed $max Maximal value
29
     *                      Or can be an array of values
30
     *
31
     * @return array|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
34
     */
35 32
    public static function randBetween(mixed $min, mixed $max): array|string|int
36
    {
37 32
        if (is_array($min) || is_array($max)) {
38 21
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $min, $max);
39
        }
40
41
        try {
42 32
            $min = (int) Helpers::validateNumericNullBool($min);
43 31
            $max = (int) Helpers::validateNumericNullBool($max);
44 30
            Helpers::validateNotNegative($max - $min);
45 4
        } catch (Exception $e) {
46 4
            return $e->getMessage();
47
        }
48
49 28
        return mt_rand($min, $max);
50
    }
51
52
    /**
53
     * RANDARRAY.
54
     *
55
     * Generates a list of sequential numbers in an array.
56
     *
57
     * Excel Function:
58
     *      RANDARRAY([rows],[columns],[start],[step])
59
     *
60
     * @param mixed $rows the number of rows to return, defaults to 1
61
     * @param mixed $columns the number of columns to return, defaults to 1
62
     * @param mixed $min the minimum number to be returned, defaults to 0
63
     * @param mixed $max the maximum number to be returned, defaults to 1
64
     * @param bool $wholeNumber the type of numbers to return:
65
     *                             False - Decimal numbers to 15 decimal places. (default)
66
     *                             True - Whole (integer) numbers
67
     *
68
     * @return array|string The resulting array, or a string containing an error
69
     */
70 3
    public static function randArray(mixed $rows = 1, mixed $columns = 1, mixed $min = 0, mixed $max = 1, bool $wholeNumber = false): string|array
71
    {
72
        try {
73 3
            $rows = (int) Helpers::validateNumericNullSubstitution($rows, 1);
74 3
            Helpers::validatePositive($rows);
75 3
            $columns = (int) Helpers::validateNumericNullSubstitution($columns, 1);
76 3
            Helpers::validatePositive($columns);
77 3
            $min = Helpers::validateNumericNullSubstitution($min, 1);
78 3
            $max = Helpers::validateNumericNullSubstitution($max, 1);
79
80 3
            if ($max <= $min) {
81 3
                return ExcelError::VALUE();
82
            }
83 1
        } catch (Exception $e) {
84 1
            return $e->getMessage();
85
        }
86
87 2
        return array_chunk(
88 2
            array_map(
89 2
                fn (): int|float => $wholeNumber
90 1
                    ? mt_rand((int) $min, (int) $max)
91 2
                    : (mt_rand() / mt_getrandmax()) * ($max - $min) + $min,
92 2
                array_fill(0, $rows * $columns, $min)
93 2
            ),
94 2
            max($columns, 1)
95 2
        );
96
    }
97
}
98