Friday, June 9, 2017

macOS Sierra: Bash (Unix shell): Associative Array Example: Reading from CSV into Array

Command:

$ 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:

Total population: 13958220136
Random index: 4227209587
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 7805864096
Cattle (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 8511144278
Domestic pig (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 3892081176
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 2589979307
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 7946872317
Cattle (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 372258826
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 13908122684
Short-eared owl (population: 2000000)
Probability: .00014328474408006714

Result:

Total population: 13958220136
Random index: 7247815301
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 9352855300
Domestic sheep (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 9642135454
Domestic sheep (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 11711491958
Cat (population: 600000000)
Probability: .04298542322402014307

Result:

Total population: 13958220136
Random index: 3990365909
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 272907397
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 9041133545
Domestic pig (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 7777621570
Cattle (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 9017444984
Domestic pig (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 4118816981
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 3797333834
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 12061584807
Mourning dove (population: 475000000)
Probability: .03403012671901594660

Result:

Total population: 13958220136
Random index: 12170053267
Mourning dove (population: 475000000)
Probability: .03403012671901594660

Result:

Total population: 13958220136
Random index: 531916373
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 10657946193
Domestic goat (population: 850000000)
Probability: .06089601623402853602

Result:

Total population: 13958220136
Random index: 13481628295
Collared trogon (population: 50000000)
Probability: .00358211860200167858

Result:

Total population: 13958220136
Random index: 9130914826
Domestic pig (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 7627418145
Cattle (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 12225242832
Mourning dove (population: 475000000)
Probability: .03403012671901594660

Result:

Total population: 13958220136
Random index: 3731595661
Human (population: 7350000000)
Probability: .52657143449424675272

Result:

Total population: 13958220136
Random index: 11796334858
Cat (population: 600000000)
Probability: .04298542322402014307

Result:

Total population: 13958220136
Random index: 7477886491
Cattle (population: 1000000000)
Probability: .07164237204003357179

Result:

Total population: 13958220136
Random index: 760089938
Human (population: 7350000000)
Probability: .52657143449424675272

Thursday, June 8, 2017

凍った湖に銃弾を撃つとどうなるか知ってますか?

macOS Sierra: Bash (Unix shell): Associative Array Example: Random Bird (using List of birds by population)

Command:

$ 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

Juan Enriquez: Your online life, permanent as a tattoo

Tuesday, June 6, 2017

macOS Sierra: Bash (Unix shell): Generating Random Number in Arbitrary Range

Command (Random number between 0 to 120,000,000):

$ MIN=0; MAX=120000000; while rnd=$(cat /dev/urandom | LC_CTYPE=C tr -dc 0-9 | fold -w${#MAX} | head -1 | sed 's/^0*//;'); [ -z $rnd ] && rnd=0; (( $rnd < $MIN || $rnd > $MAX)); do :; done; echo $rnd;


Result:

74774521

macOS Sierra: Bash (Unix shell): Command to Test for Int64 or Long Integer Support

Command:

$ ((x=(2**63)-1)); echo $x; ((x++)); echo $x;


Result:

9223372036854775807
-9223372036854775808


Interesting note:

9,223,372,036,854,775,807

is

nine quintillion two hundred twenty three quadrillion three hundred seventy two trillion thirty six billion eight hundred fifty four million seven hundred seventy five thousand eight hundred seven

is

九百二十二三千三百七十二三百六十八五千四百七十七五千八百七

Takuma Sato: WISH TV: On His Big Win

Takuma Sato