2 # Visualize how random the $RANDOM function is
3 # while demonstrating some neat array functions and things
5 # Check for sane parameter
6 if [[ "${1}" -lt 50 ]]; then
7 echo "Please input integer higher than 50"
16 for ((n
=0;n
<${1};n
++)); do
17 # Assign random number, use modulo to reduce to desired range, add 1 to get rid of 0
18 Rand
=$
(( ( RANDOM
% 8 ) + 1 ))
19 # Our Count array needs to be increased
20 (( Count
[${Rand}]++ ))
21 # And append a character to our graph
27 for n
in "${Count[@]}"; do
28 (( n
> Max
)) && Max
=${n}
32 for n
in "${Count[@]}"; do
33 (( n
< Min
)) && Min
=${n}
37 Mean
=$
(bc <<< "scale=2;${1}/${#Count[@]}")
39 # Unset Graph array if we can't get it to fit on screen
43 if [[ "${Max}" -gt "${MaxWidth}" ]]; then
47 # Loop through the Count array, note the # which returns the size of the array for us
48 for ((n
=1;n
<=${#Count[@]};n
++)); do
49 # printf to get some better formatting (space padding)
50 printf "%-3s %-5s %s\n" ${n} ${Count[$n]} ${Graph[$n]}
52 echo -e "\nMin: ${Min}, Max: ${Max}, Mean: ${Mean}"