PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Calculation / MathTrig / Random.php

Random.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/MathTrig/Random.php

98 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception;
7 use TablePress\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()
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<mixed>|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($min, $max)
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<mixed>|string The resulting array, or a string containing an error
69 */
70 public static function randArray($rows = 1, $columns = 1, $min = 0, $max = 1, bool $wholeNumber = false)
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 () => $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 }
98