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 less information for eg. Travis or CircleCI to avoid big log file. |
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
|
|
|
* @param string $filename Output filename without extension |
38
|
|
|
* @param string $fileExtension File extension. Default is '.dat' |
39
|
|
|
* |
40
|
|
|
* @see Generate::getMinMaxNumberSize() |
41
|
|
|
* @see File::createMissingDirectory() |
42
|
|
|
* @see File::checkIfFileExists() |
43
|
|
|
*/ |
44
|
|
|
public static function generateAndSaveRandomNumbers($min, $max, $decimalPlaces, $maxFileSize, $filename, $fileExtension = '.dat') |
45
|
|
|
{ |
46
|
|
|
$range = $max - $min; |
47
|
|
|
|
48
|
|
|
$minMaxNumberSize = self::getMinMaxNumberSize($min, $max, $decimalPlaces); |
49
|
|
|
|
50
|
|
|
if ($minMaxNumberSize['max'] > $maxFileSize) { |
51
|
|
|
die('Error: Cannot generate any number due to too low file size.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
File::createMissingDirectory($filename); |
55
|
|
|
File::checkIfFileExists($filename, $fileExtension); |
56
|
|
|
|
57
|
|
|
// Maximum iteration without spaces |
58
|
|
|
$maximumIteration = (int) ($maxFileSize / $minMaxNumberSize['max']); |
59
|
|
|
Text::debug('First maximum iteration: '.$maximumIteration); |
60
|
|
|
|
61
|
|
|
$findingStart = microtime(true); |
62
|
|
|
|
63
|
|
|
Text::message('Finding maximum iteration for loop...'); |
64
|
|
|
|
65
|
|
|
for ($i = 0; ; $i++) { |
66
|
|
|
$tempMaxIteration = $maximumIteration - $i; |
67
|
|
|
$maximumBytes = ($minMaxNumberSize['max'] * $tempMaxIteration) + $tempMaxIteration - 1; |
68
|
|
|
|
69
|
|
|
if ($maxFileSize >= $maximumBytes) { |
70
|
|
|
$maximumIteration = $tempMaxIteration; |
71
|
|
|
Text::debug('Found right max iteration for loop'); |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
Text::printTimeDuration($findingStart); |
77
|
|
|
|
78
|
|
|
Text::message('GENERATING...'); |
79
|
|
|
Text::debug('Min: '.$min.' Max: '.$max.' Decimal places: '.$decimalPlaces.' Size: '.$maxFileSize."\nMaximum iteration: ".$maximumIteration."\n"); |
80
|
|
|
|
81
|
|
|
$generateStart = microtime(true); |
82
|
|
|
|
83
|
|
|
$file = fopen($filename.$fileExtension, 'w'); |
84
|
|
|
|
85
|
|
|
for ($i = 1; $i <= $maximumIteration; $i++) { |
86
|
|
|
// Print progress and move cursor back to position 0 |
87
|
|
|
if (!self::getTesting()) { |
88
|
|
|
echo 'Progress: '.$i.'/'.$maximumIteration."\r"; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Random number |
92
|
|
|
$number = $min + $range * (mt_rand() / mt_getrandmax()); |
93
|
|
|
// Format with trailing zeros ie. 8.00 |
94
|
|
|
$number = number_format((float) $number, (int) $decimalPlaces, '.', ''); |
95
|
|
|
|
96
|
|
|
$outputString = $number.' '; |
97
|
|
|
|
98
|
|
|
// Remove last space |
99
|
|
|
if ($i === $maximumIteration) { |
100
|
|
|
$outputString = trim($outputString); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
fwrite($file, $outputString); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
fclose($file); |
107
|
|
|
|
108
|
|
|
Text::printTimeDuration($generateStart); |
109
|
|
|
Text::message('Output file '.$filename.$fileExtension.' generated with '.filesize($filename.$fileExtension).' bytes.'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Switch min and max if min > max. |
114
|
|
|
* |
115
|
|
|
* @param int|float $min Minimum allowed number to generate |
116
|
|
|
* @param int|float $max Maximum allowed number to generate |
117
|
|
|
* @param int|float $decimalPlaces Number of decimal places |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
private static function getMinMaxNumberSize($min, $max, $decimalPlaces) |
122
|
|
|
{ |
123
|
|
|
$additionalBytes = null; |
124
|
|
|
|
125
|
|
|
if ((int) $decimalPlaces > 0) { |
126
|
|
|
// + 1 for decimal point |
127
|
|
|
$additionalBytes = (int) $decimalPlaces + 1; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$minimumNumberSize = min(strlen((int) $min), strlen((int) $max)) + $additionalBytes; |
131
|
|
|
$maximumNumberSize = max(strlen((int) $min), strlen((int) $max)) + $additionalBytes; |
132
|
|
|
|
133
|
|
|
Text::debug('Minimum number size: '.$minimumNumberSize); |
134
|
|
|
Text::debug('Maximum number size: '.$maximumNumberSize); |
135
|
|
|
|
136
|
|
|
return array('min' => $minimumNumberSize, 'max' => $maximumNumberSize); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Switch min and max if min > max. |
141
|
|
|
* |
142
|
|
|
* @param float $min Minimum |
143
|
|
|
* @param float $max Maximum |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public static function checkSwitchMinMax($min, $max) |
148
|
|
|
{ |
149
|
|
|
if ($min > $max) { |
150
|
|
|
Text::debug('!! Switching min and max !!'); |
151
|
|
|
|
152
|
|
|
$tempMin = $min; |
153
|
|
|
|
154
|
|
|
$min = $max; |
155
|
|
|
$max = $tempMin; |
156
|
|
|
|
157
|
|
|
Text::debug('Min: '.$min); |
158
|
|
|
Text::debug('Max: '.$max); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return array('min' => $min, 'max' => $max); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set $testingMode property value. |
166
|
|
|
* |
167
|
|
|
* @param bool $value |
168
|
|
|
* |
169
|
|
|
* @see Generate::$testingMode |
170
|
|
|
*/ |
171
|
|
|
public static function setTesting($value) |
172
|
|
|
{ |
173
|
|
|
self::$testingMode = $value; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get $testingMode property value. |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
* |
181
|
|
|
* @see Generate::$testingMode |
182
|
|
|
*/ |
183
|
|
|
public static function getTesting() |
184
|
|
|
{ |
185
|
|
|
return self::$testingMode; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|