1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Includes/Generate.php. |
4
|
|
|
* |
5
|
|
|
* @author Adam "Saibamen" Stachowicz <[email protected]> |
6
|
|
|
* @license MIT |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/Saibamen/Generate-Sort-Numbers |
9
|
|
|
*/ |
10
|
|
|
/** |
11
|
|
|
* Functions for 'Generate' script. |
12
|
|
|
* |
13
|
|
|
* @author Adam "Saibamen" Stachowicz <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Includes; |
17
|
|
|
|
18
|
|
|
require_once 'Text.php'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Functions for 'Generate' script. |
22
|
|
|
*/ |
23
|
|
|
class Generate |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var bool If true - print more information. |
27
|
|
|
*/ |
28
|
|
|
private static $testingMode = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Generate string with randomized numbers. |
32
|
|
|
* |
33
|
|
|
* @param int|float $min Minimum allowed number to generate |
34
|
|
|
* @param int|float $max Maximum allowed number to generate |
35
|
|
|
* @param int|float $decimalPlaces Number of decimal places |
36
|
|
|
* @param int $maxFileSize Maximum file size in bytes |
37
|
|
|
* |
38
|
|
|
* @return string Generated string without trailing spaces |
39
|
|
|
*/ |
40
|
|
|
public static function generateRandomNumbers($min, $max, $decimalPlaces, $maxFileSize) |
41
|
|
|
{ |
42
|
|
|
$range = $max - $min; |
43
|
|
|
$outputString = ''; |
44
|
|
|
|
45
|
|
|
$minMaxNumberSize = self::getMinMaxNumberSize($min, $max, $decimalPlaces); |
46
|
|
|
|
47
|
|
|
if ($minMaxNumberSize['max'] > $maxFileSize) { |
48
|
|
|
die('Error: Cannot generate any number due to too low file size.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Maximum iteration without spaces |
52
|
|
|
$maximumIteration = (int) ($maxFileSize / $minMaxNumberSize['max']); |
53
|
|
|
Text::debug('First maximum iteration: '.$maximumIteration); |
54
|
|
|
|
55
|
|
|
$findingStart = microtime(true); |
56
|
|
|
|
57
|
|
|
Text::message('Finding maximum iteration for loop...'); |
58
|
|
|
|
59
|
|
|
for ($i = 0; ; $i++) { |
60
|
|
|
$tempMaxIteration = $maximumIteration - $i; |
61
|
|
|
$maximumBytes = ($minMaxNumberSize['max'] * $tempMaxIteration) + $tempMaxIteration - 1; |
62
|
|
|
|
63
|
|
|
if ($maxFileSize >= $maximumBytes) { |
64
|
|
|
$maximumIteration = $tempMaxIteration; |
65
|
|
|
Text::debug('Found right max iteration for loop'); |
66
|
|
|
break; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
Text::printTimeDuration($findingStart); |
71
|
|
|
|
72
|
|
|
Text::message('GENERATING...'); |
73
|
|
|
Text::debug('Min: '.$min.' Max: '.$max.' Decimal places: '.$decimalPlaces.' Size: '.$maxFileSize."\nMaximum iteration: ".$maximumIteration."\n"); |
74
|
|
|
|
75
|
|
|
$generateStart = microtime(true); |
76
|
|
|
|
77
|
|
|
for ($i = 1; $i <= $maximumIteration; $i++) { |
78
|
|
|
// Print progress and move cursor back to position 0 |
79
|
|
|
if (!self::getTesting()) { |
80
|
|
|
echo 'Progress: '.$i.'/'.$maximumIteration."\r"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Random number |
84
|
|
|
$number = $min + $range * (mt_rand() / mt_getrandmax()); |
85
|
|
|
// Format with trailing zeros ie. 8.00 |
86
|
|
|
$number = number_format((float) $number, (int) $decimalPlaces, '.', ''); |
87
|
|
|
$outputString .= $number.' '; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
Text::printTimeDuration($generateStart); |
91
|
|
|
Text::debug('Output string has '.strlen(trim($outputString)).' bytes.'); |
92
|
|
|
|
93
|
|
|
// Remove last space |
94
|
|
|
return trim($outputString); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Switch min and max if min > max. |
99
|
|
|
* |
100
|
|
|
* @param int|float $min Minimum allowed number to generate |
101
|
|
|
* @param int|float $max Maximum allowed number to generate |
102
|
|
|
* @param int|float $decimalPlaces Number of decimal places |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public static function getMinMaxNumberSize($min, $max, $decimalPlaces) |
107
|
|
|
{ |
108
|
|
|
$additionalBytes = null; |
109
|
|
|
|
110
|
|
|
if ((int) $decimalPlaces > 0) { |
111
|
|
|
// + 1 for decimal point |
112
|
|
|
$additionalBytes = (int) $decimalPlaces + 1; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$minimumNumberSize = min(strlen((int) $min), strlen((int) $max)) + $additionalBytes; |
116
|
|
|
$maximumNumberSize = max(strlen((int) $min), strlen((int) $max)) + $additionalBytes; |
117
|
|
|
|
118
|
|
|
Text::debug('Minimum number size: '.$minimumNumberSize); |
119
|
|
|
Text::debug('Maximum number size: '.$maximumNumberSize); |
120
|
|
|
|
121
|
|
|
return array('min' => $minimumNumberSize, 'max' => $maximumNumberSize); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Switch min and max if min > max. |
126
|
|
|
* |
127
|
|
|
* @param float $min Minimum |
128
|
|
|
* @param float $max Maximum |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public static function checkSwitchMinMax($min, $max) |
133
|
|
|
{ |
134
|
|
|
if ($min > $max) { |
135
|
|
|
Text::debug('!! Switching min and max !!'); |
136
|
|
|
|
137
|
|
|
$tempMin = $min; |
138
|
|
|
|
139
|
|
|
$min = $max; |
140
|
|
|
$max = $tempMin; |
141
|
|
|
|
142
|
|
|
Text::debug('Min: '.$min); |
143
|
|
|
Text::debug('Max: '.$max); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return array('min' => $min, 'max' => $max); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set $testingMode property value. |
151
|
|
|
* |
152
|
|
|
* @param bool $value |
153
|
|
|
* |
154
|
|
|
* @see Generate::$testingMode |
155
|
|
|
*/ |
156
|
|
|
public static function setTesting($value) |
157
|
|
|
{ |
158
|
|
|
self::$testingMode = $value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get $testingMode property value. |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
* |
166
|
|
|
* @see Generate::$testingMode |
167
|
|
|
*/ |
168
|
|
|
public static function getTesting() |
169
|
|
|
{ |
170
|
|
|
return self::$testingMode; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|