Created
July 21, 2019 07:58
-
-
Save theycallmemac/f66b0afeca215df97869dd28612bea74 to your computer and use it in GitHub Desktop.
A short bash script to simulate a coinflip.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
FLIP=$(($(($RANDOM%10))%2)) | |
if [ $FLIP -eq 1 ];then | |
echo "heads" | |
else | |
echo "tails" | |
fi |
Also to simply the conditional syntax by chaining boolean logic, and removing redundant syntax you can get it pretty clean:
#!/bin/bash
(( RANDOM % 2 )) && echo "heads" || echo "tails"
@srozanc-mcs ah, that works because the value returned by (()) is also the return code! (That must also be why the set
option to bail on any error may unexpectedly bail on a line that includes (()), depending on the result.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why do you mod 10 and then take that and mod 2 instead of just modding 2 straight off the bat, as the last bit should be evenly distributed in the set of 0 to 32767 inclusive?