📄 random-test.sh
字号:
#!/bin/bash# How random is RANDOM?RANDOM=$$ # Reseed the random number generator using script process ID.PIPS=6 # A die has 6 pips.MAXTHROWS=600 # Increase this if you have nothing better to do with your time.throw=0 # Throw count.ones=0 # Must initialize counts to zero,twos=0 #+ since an uninitialized variable is null, not zero.threes=0fours=0fives=0sixes=0print_result (){echoecho "ones = $ones"echo "twos = $twos"echo "threes = $threes"echo "fours = $fours"echo "fives = $fives"echo "sixes = $sixes"echo}update_count(){case "$1" in 0) let "ones += 1";; # Since die has no "zero", this corresponds to 1. 1) let "twos += 1";; # And this to 2, etc. 2) let "threes += 1";; 3) let "fours += 1";; 4) let "fives += 1";; 5) let "sixes += 1";;esac}echowhile [ "$throw" -lt "$MAXTHROWS" ]do let "die1 = RANDOM % $PIPS" update_count $die1 let "throw += 1"done print_resultexit 0# The scores should distribute fairly evenly, assuming RANDOM is fairly random.# With $MAXTHROWS at 600, all should cluster around 100, plus-or-minus 20 or so.## Keep in mind that RANDOM is a pseudorandom generator,#+ and not a spectacularly good one at that.# Randomness is a deep and complex subject.# Sufficiently long "random" sequences may exhibit#+ chaotic and other "non-random" behavior.# Exercise (easy):# ---------------# Rewrite this script to flip a coin 1000 times.# Choices are "HEADS" and "TAILS".
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -