$ cat associative_array2.sh
Result:
#!/bin/bash
aa=(
'Mallard::17000000'
'Chimney swift::15000000'
'European nightjar::6000000'
'Thick-billed murre::22000000'
'White-faced ibis::1200000'
'Rock dove::260000000'
'Rainbow bee-eater::1000000'
'Common cuckoo::100000000'
'Common Kestrel::5000000'
'Willow ptarmigan::40000000'
'Pacific loon::1600000'
'American coot::6000000'
'Red-footed booby::1000000'
'Lesser flamingo::3240000'
'Great spotted woodpecker::216000000'
'Black-necked grebe::42000000'
'Short-tailed shearwater::23000000'
'Red-tailed black cockatoo::100000'
'Macaroni penguin::18000000'
'Short-eared owl::2000000'
'Southern brown kiwi::29800'
'Little tinamou::4999999'
'Collared trogon::50000000'
'Laysan duck::521'
'Juan Fernández firecrown::3000'
'Puerto Rican nightjar::2000'
'Shore dotterel::250'
"Storm's stork::500"
'Socorro dove::100'
'Narcondam hornbill::340'
'Black-hooded coucal::70'
'Madagascar fish eagle::360'
'Bornean peacock-pheasant::2499'
'Yellow-billed loon::32000'
'Lord Howe woodhen::230'
'Flightless cormorant::1679'
'Andean flamingo::38000'
'Ivory-billed woodpecker::50'
'New Zealand grebe::2000'
'New Zealand storm petrel::50'
'Kakapo::126'
'Galapagos penguin::1800'
'Forest owlet::400'
'Little spotted kiwi::1200'
'Slaty-breasted tinamou::49999'
'Javan trogon::1500'
)
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_array2.sh
Result:
Total population: 835308473
Random index: 763607797
Macaroni penguin (population: 18000000)
Probability: .02154892543511886536
Result:
Total population: 835308473
Random index: 442019183
Willow ptarmigan (population: 40000000)
Probability: .04788650096693081191
Result:
Total population: 835308473
Random index: 600337171
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435
Result:
Total population: 835308473
Random index: 542347304
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435
Result:
Total population: 835308473
Random index: 399097554
Common cuckoo (population: 100000000)
Probability: .11971625241732702979
Result:
Total population: 835308473
Random index: 602221971
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435
Result:
Total population: 835308473
Random index: 31407682
Chimney swift (population: 15000000)
Probability: .01795743786259905446
Result:
Total population: 835308473
Random index: 471711629
American coot (population: 6000000)
Probability: .00718297514503962178
Result:
Total population: 835308473
Random index: 127100259
Rock dove (population: 260000000)
Probability: .31126225628505027746
Result:
Total population: 835308473
Random index: 380012920
Common cuckoo (population: 100000000)
Probability: .11971625241732702979
Result:
Total population: 835308473
Random index: 394531514
Common cuckoo (population: 100000000)
Probability: .11971625241732702979
Result:
Total population: 835308473
Random index: 78048210
Rock dove (population: 260000000)
Probability: .31126225628505027746
Result:
Total population: 835308473
Random index: 33341909
European nightjar (population: 6000000)
Probability: .00718297514503962178
Result:
Total population: 835308473
Random index: 211802308
Rock dove (population: 260000000)
Probability: .31126225628505027746
Result:
Total population: 835308473
Random index: 94119789
Rock dove (population: 260000000)
Probability: .31126225628505027746
Result:
Total population: 835308473
Random index: 359620354
Common cuckoo (population: 100000000)
Probability: .11971625241732702979
Result:
Total population: 835308473
Random index: 429592312
Willow ptarmigan (population: 40000000)
Probability: .04788650096693081191
Result:
Total population: 835308473
Random index: 199643906
Rock dove (population: 260000000)
Probability: .31126225628505027746
Result:
Total population: 835308473
Random index: 656519406
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435
Result:
Total population: 835308473
Random index: 448395870
Willow ptarmigan (population: 40000000)
Probability: .04788650096693081191
Result:
Total population: 835308473
Random index: 392079604
Common cuckoo (population: 100000000)
Probability: .11971625241732702979
Result:
Total population: 835308473
Random index: 61798025
Rock dove (population: 260000000)
Probability: .31126225628505027746
Result:
Total population: 835308473
Random index: 778932643
Short-eared owl (population: 2000000)
Probability: .00239432504834654059
Result:
Total population: 835308473
Random index: 393661406
Common cuckoo (population: 100000000)
Probability: .11971625241732702979
Result:
Total population: 835308473
Random index: 88104221
Rock dove (population: 260000000)
Probability: .31126225628505027746
Result:
Total population: 835308473
Random index: 623477130
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435
Result:
Total population: 835308473
Random index: 401873132
Common cuckoo (population: 100000000)
Probability: .11971625241732702979
Result:
Total population: 835308473
Random index: 680185574
Great spotted woodpecker (population: 216000000)
Probability: .25858710522142638435
Result:
Total population: 835308473
Random index: 724901495
Black-necked grebe (population: 42000000)
Probability: .05028082601527735251
Result:
Total population: 835308473
Random index: 281804665
Rock dove (population: 260000000)
Probability: .31126225628505027746