| 1441 |
ariadna |
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 |
public static function rand(): int|float
|
|
|
19 |
{
|
|
|
20 |
return mt_rand(0, 10000000) / 10000000;
|
|
|
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 |
public static function randBetween(mixed $min, mixed $max): array|string|int
|
|
|
36 |
{
|
|
|
37 |
if (is_array($min) || is_array($max)) {
|
|
|
38 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $min, $max);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
try {
|
|
|
42 |
$min = (int) Helpers::validateNumericNullBool($min);
|
|
|
43 |
$max = (int) Helpers::validateNumericNullBool($max);
|
|
|
44 |
Helpers::validateNotNegative($max - $min);
|
|
|
45 |
} catch (Exception $e) {
|
|
|
46 |
return $e->getMessage();
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
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 |
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 |
$rows = (int) Helpers::validateNumericNullSubstitution($rows, 1);
|
|
|
74 |
Helpers::validatePositive($rows);
|
|
|
75 |
$columns = (int) Helpers::validateNumericNullSubstitution($columns, 1);
|
|
|
76 |
Helpers::validatePositive($columns);
|
|
|
77 |
$min = Helpers::validateNumericNullSubstitution($min, 1);
|
|
|
78 |
$max = Helpers::validateNumericNullSubstitution($max, 1);
|
|
|
79 |
|
|
|
80 |
if ($max <= $min) {
|
|
|
81 |
return ExcelError::VALUE();
|
|
|
82 |
}
|
|
|
83 |
} catch (Exception $e) {
|
|
|
84 |
return $e->getMessage();
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return array_chunk(
|
|
|
88 |
array_map(
|
|
|
89 |
fn (): int|float => $wholeNumber
|
|
|
90 |
? mt_rand((int) $min, (int) $max)
|
|
|
91 |
: (mt_rand() / mt_getrandmax()) * ($max - $min) + $min,
|
|
|
92 |
array_fill(0, $rows * $columns, $min)
|
|
|
93 |
),
|
|
|
94 |
max($columns, 1)
|
|
|
95 |
);
|
|
|
96 |
}
|
|
|
97 |
}
|