|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Functions for 'Generate' script. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Adam "Saibamen" Stachowicz <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** Functions for inputs, saving file and printing text */ |
|
9
|
|
|
require_once 'console.inc.php'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Generate string with randomized numbers. |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $min Minimum allowed number to generate |
|
15
|
|
|
* @param string $max Maximum allowed number to generate |
|
16
|
|
|
* @param string $decimalPlaces Number of decimal places |
|
17
|
|
|
* |
|
18
|
|
|
* @return string Generated string without trailing spaces |
|
19
|
|
|
*/ |
|
20
|
|
|
function generateRandomNumbers($min, $max, $decimalPlaces) |
|
21
|
|
|
{ |
|
22
|
|
|
text('GENERATING...'); |
|
23
|
|
|
debug("Generating string.\nMin: ".$min.' Max: '.$max.' Decimal places: '.$decimalPlaces); |
|
24
|
|
|
|
|
25
|
|
|
$range = $max - $min; |
|
26
|
|
|
$outputString = ''; |
|
27
|
|
|
$howManyIterations = 10; |
|
28
|
|
|
|
|
29
|
|
|
$GENERATE_START = microtime(true); |
|
30
|
|
|
|
|
31
|
|
|
// TODO: Show percent progress based on iteration |
|
32
|
|
|
// TODO: Calculate output size for generate |
|
33
|
|
|
for ($i = 0; $i <= $howManyIterations; $i++) { |
|
34
|
|
|
// Print progress and move cursor back to position 0 |
|
35
|
|
|
echo 'Progress: '.$i.'/'.$howManyIterations."\r"; |
|
36
|
|
|
|
|
37
|
|
|
$number = $min + $range * (mt_rand() / mt_getrandmax()); |
|
38
|
|
|
// Format with trailing zeros ie. 8.00 |
|
39
|
|
|
$number = number_format((float) $number, (int) $decimalPlaces, '.', ''); |
|
40
|
|
|
$outputString .= $number.' '; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
printEndTime($GENERATE_START); |
|
|
|
|
|
|
44
|
|
|
debug($outputString); |
|
45
|
|
|
|
|
46
|
|
|
// Remove last space |
|
47
|
|
|
return trim($outputString); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get number from User. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $message Message for User what he must type |
|
54
|
|
|
* @param int $default Default number for empty input. Default is 0 |
|
55
|
|
|
* |
|
56
|
|
|
* @return string Inserted number |
|
57
|
|
|
*/ |
|
58
|
|
|
function getNumberInput($message, $default = 0) |
|
59
|
|
|
{ |
|
60
|
|
|
echo $message.' [Default: '.$default.']: '; |
|
61
|
|
|
|
|
62
|
|
|
do { |
|
63
|
|
|
$input = trim(fgets(STDIN)); |
|
|
|
|
|
|
64
|
|
|
debug('User input: '.$input); |
|
65
|
|
|
|
|
66
|
|
|
if (is_null($input) || empty($input)) { |
|
67
|
|
|
debug('Using default input: '. $default); |
|
68
|
|
|
$input = $default; |
|
69
|
|
|
} elseif (!is_numeric($input)) { |
|
70
|
|
|
echo 'Please input number: '; |
|
71
|
|
|
} |
|
72
|
|
|
} while (!is_numeric($input)); |
|
73
|
|
|
|
|
74
|
|
|
return $input; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Switch Globals min and max if min > max. |
|
79
|
|
|
*/ |
|
80
|
|
|
function checkSwitchGlobalMinMax() |
|
81
|
|
|
{ |
|
82
|
|
|
if ($GLOBALS['min'] > $GLOBALS['max']) { |
|
83
|
|
|
debug('!! Switching min and max !!'); |
|
84
|
|
|
|
|
85
|
|
|
$tempMin = $GLOBALS['min']; |
|
86
|
|
|
|
|
87
|
|
|
$GLOBALS['min'] = $GLOBALS['max']; |
|
88
|
|
|
$GLOBALS['max'] = $tempMin; |
|
89
|
|
|
|
|
90
|
|
|
debug('Min: '.$GLOBALS['min']); |
|
91
|
|
|
debug('Max: '.$GLOBALS['max']); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|