$ cat associative_array3.sh
Result:
#!/bin/bash
aa=()
while IFS=, read -ra line; do if [ -n "${line[0]}" ]; then aa+=("${line[0]}::${line[3]}");fi;done < List\ of\ organisms\ by\ population.csv
total=0
for k in "${aa[@]}"
do
value="${k##*::}"
total=$((total + value))
done
echo "Total population: $total"
LC_CTYPE=C; MIN=1; MAX=$total; while rnd=$(cat /dev/urandom | tr -dc 0-9 | fold -w${#MAX} | head -1 | sed 's/^0*//;'); [ -z $rnd ] && rnd=0; (( $rnd < $MIN || $rnd > $MAX)); do :; done;
echo "Random index: $rnd"
index=$rnd
current=0
for k in "${aa[@]}"
do
value="${k##*::}"
current=$((current + value))
if [ $index -le $current ]
then
name="${k%%::*}"
population=$value
break
fi
done
echo "$name (population: $population)"
printf "Probability: "
echo "$population/$total" | bc -l
Command:
$ ./associative_array3.sh
Result:
Total population: 13958220136
Random index: 8955227583
Domestic pig (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 4227209587
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 7805864096
Cattle (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 8511144278
Domestic pig (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 3892081176
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 2589979307
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 7946872317
Cattle (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 372258826
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 13908122684
Short-eared owl (population: 2000000)
Probability: .00014328474408006714
Result:
Random index: 7247815301
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 9352855300
Domestic sheep (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 9642135454
Domestic sheep (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 11711491958
Cat (population: 600000000)
Probability: .04298542322402014307
Result:
Random index: 3990365909
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 272907397
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 9041133545
Domestic pig (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 7777621570
Cattle (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 9017444984
Domestic pig (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 4118816981
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 3797333834
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 12061584807
Mourning dove (population: 475000000)
Probability: .03403012671901594660
Result:
Random index: 12170053267
Mourning dove (population: 475000000)
Probability: .03403012671901594660
Result:
Random index: 531916373
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 10657946193
Domestic goat (population: 850000000)
Probability: .06089601623402853602
Result:
Random index: 13481628295
Collared trogon (population: 50000000)
Probability: .00358211860200167858
Result:
Random index: 9130914826
Domestic pig (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 7627418145
Cattle (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 12225242832
Mourning dove (population: 475000000)
Probability: .03403012671901594660
Result:
Random index: 3731595661
Human (population: 7350000000)
Probability: .52657143449424675272
Result:
Random index: 11796334858
Cat (population: 600000000)
Probability: .04298542322402014307
Result:
Random index: 7477886491
Cattle (population: 1000000000)
Probability: .07164237204003357179
Result:
Random index: 760089938
Human (population: 7350000000)
Probability: .52657143449424675272