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
|
|
|
* Generate string with randomized numbers. |
27
|
|
|
* |
28
|
|
|
* @param int|float $min Minimum allowed number to generate |
29
|
|
|
* @param int|float $max Maximum allowed number to generate |
30
|
|
|
* @param int|float $decimalPlaces Number of decimal places |
31
|
|
|
* @param int $maxFileSize Maximum file size in bytes |
32
|
|
|
* |
33
|
|
|
* @return string Generated string without trailing spaces |
34
|
|
|
*/ |
35
|
|
|
public static function generateRandomNumbers($min, $max, $decimalPlaces, $maxFileSize) |
36
|
|
|
{ |
37
|
|
|
$range = $max - $min; |
38
|
|
|
$outputString = ''; |
39
|
|
|
|
40
|
|
|
$minMaxNumberSize = self::getMinMaxNumberSize($min, $max, $decimalPlaces); |
41
|
|
|
|
42
|
|
|
if ($minMaxNumberSize['max'] > $maxFileSize) { |
43
|
|
|
die('Error: Cannot generate any number due to too low file size.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Maximum iteration without spaces |
47
|
|
|
$maximumIteration = (int) ($maxFileSize / $minMaxNumberSize['max']); |
48
|
|
|
Text::debug('First maximum iteration: '.$maximumIteration); |
49
|
|
|
|
50
|
|
|
$findingStart = microtime(true); |
51
|
|
|
|
52
|
|
|
Text::message('Finding maximum iteration for loop...'); |
53
|
|
|
|
54
|
|
|
for ($i = 0; ; $i++) { |
55
|
|
|
$tempMaxIteration = $maximumIteration - $i; |
56
|
|
|
$maximumBytes = ($minMaxNumberSize['max'] * $tempMaxIteration) + $tempMaxIteration - 1; |
57
|
|
|
|
58
|
|
|
if ($maxFileSize >= $maximumBytes) { |
59
|
|
|
$maximumIteration = $tempMaxIteration; |
60
|
|
|
Text::debug('Found right max iteration for loop'); |
61
|
|
|
break; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
Text::printTimeDuration($findingStart); |
66
|
|
|
|
67
|
|
|
Text::message('GENERATING...'); |
68
|
|
|
Text::debug('Min: '.$min.' Max: '.$max.' Decimal places: '.$decimalPlaces.' Size: '.$maxFileSize."\nMaximum iteration: ".$maximumIteration."\n"); |
69
|
|
|
|
70
|
|
|
$generateStart = microtime(true); |
71
|
|
|
|
72
|
|
|
for ($i = 1; $i <= $maximumIteration; $i++) { |
73
|
|
|
// Print progress and move cursor back to position 0 |
74
|
|
|
echo 'Progress: '.$i.'/'.$maximumIteration."\r"; |
75
|
|
|
|
76
|
|
|
// Random number |
77
|
|
|
$number = $min + $range * (mt_rand() / mt_getrandmax()); |
78
|
|
|
// Format with trailing zeros ie. 8.00 |
79
|
|
|
$number = number_format((float) $number, (int) $decimalPlaces, '.', ''); |
80
|
|
|
$outputString .= $number.' '; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
Text::printTimeDuration($generateStart); |
84
|
|
|
Text::debug('Output string has '.strlen(trim($outputString)).' bytes.'); |
85
|
|
|
|
86
|
|
|
// Remove last space |
87
|
|
|
return trim($outputString); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Switch min and max if min > max. |
92
|
|
|
* |
93
|
|
|
* @param int|float $min Minimum allowed number to generate |
94
|
|
|
* @param int|float $max Maximum allowed number to generate |
95
|
|
|
* @param int|float $decimalPlaces Number of decimal places |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public static function getMinMaxNumberSize($min, $max, $decimalPlaces) |
100
|
|
|
{ |
101
|
|
|
$additionalBytes = NULL; |
102
|
|
|
|
103
|
|
|
if ((int) $decimalPlaces > 0) { |
104
|
|
|
// + 1 for decimal point |
105
|
|
|
$additionalBytes = (int) $decimalPlaces + 1; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$minimumNumberSize = min(strlen((int) $min), strlen((int) $max)) + $additionalBytes; |
109
|
|
|
$maximumNumberSize = max(strlen((int) $min), strlen((int) $max)) + $additionalBytes; |
110
|
|
|
|
111
|
|
|
Text::debug('Minimum number size: '.$minimumNumberSize); |
112
|
|
|
Text::debug('Maximum number size: '.$maximumNumberSize); |
113
|
|
|
|
114
|
|
|
return array('min' => $minimumNumberSize, 'max' => $maximumNumberSize); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Switch min and max if min > max. |
119
|
|
|
* |
120
|
|
|
* @param float $min Minimum |
121
|
|
|
* @param float $max Maximum |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public static function checkSwitchMinMax($min, $max) |
126
|
|
|
{ |
127
|
|
|
if ($min > $max) { |
128
|
|
|
Text::debug('!! Switching min and max !!'); |
129
|
|
|
|
130
|
|
|
$tempMin = $min; |
131
|
|
|
|
132
|
|
|
$min = $max; |
133
|
|
|
$max = $tempMin; |
134
|
|
|
|
135
|
|
|
Text::debug('Min: '.$min); |
136
|
|
|
Text::debug('Max: '.$max); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return array('min' => $min, 'max' => $max); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|