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

Saturday, June 3, 2017

macOS Sierra: Generating Integers with Arbitrary Size

Command (Testing with 15 digits):

$ rnd=$(cat /dev/urandom | LC_CTYPE=C tr -dc 0-9 | fold -w15 | head -1 | sed 's/^0*//;'); echo $rnd; echo $(($rnd / 2));


Result (Second line is the number half the size of the first line to test if arithmetic operation works):

585206567422781
292603283711390

Friday, June 2, 2017

macOS Sierra: Bash (Unix shell): Associative Array Example

Command:

$ cat associative_array.sh


Result:

#!/bin/bash

aa=(
'スライム::100'
'キメラ::10'
'犬::20'
'猫::80'
)

total=0

for k in "${aa[@]}"
do
value="${k##*::}"
total=$((total + value))
done

echo "Total: $total"

index=$1

current=0

for k in "${aa[@]}"
do
value="${k##*::}"
current=$((current + value))

if [ $index -le $current ]
then
name="${k%%::*}"
echo "Index $index: $name"
break
fi
done


Commands & Results:

$ ./associative_array.sh 0
Total: 210
Index 0: スライム
$ ./associative_array.sh 1
Total: 210
Index 1: スライム
$ ./associative_array.sh 10
Total: 210
Index 10: スライム
$ ./associative_array.sh 99
Total: 210
Index 99: スライム
$ ./associative_array.sh 100
Total: 210
Index 100: スライム
$ ./associative_array.sh 101
Total: 210
Index 101: キメラ
$ ./associative_array.sh 110
Total: 210
Index 110: キメラ
$ ./associative_array.sh 111
Total: 210
Index 111: 犬
$ ./associative_array.sh 130
Total: 210
Index 130: 犬
$ ./associative_array.sh 131
Total: 210
Index 131: 猫
$ ./associative_array.sh 200
Total: 210
Index 200: 猫
$ ./associative_array.sh 209
Total: 210
Index 209: 猫
$ ./associative_array.sh 210
Total: 210
Index 210: 猫
$ ./associative_array.sh 211
Total: 210

ジョーブログ【クレイジーチャレンジャー】: 【突撃】北朝鮮に潜入!平壌の街の今を公開します!

Microsoft Brings Glass back to Windows with "Project Neon"