Kode: Vælg alt
chr@chr:~$ /home/chr/die.sh
ones = 166607
twos = 165683
threes = 166465
fours = 166924
fives = 166912
sixes = 167409
chr@chr:~$
Jeg prøver lige at se om min pc koger af, hvis at jeg prøver med 1.000.000.000
Kode: Vælg alt
chr@chr:~$ /home/chr/die.sh
ones = 166607
twos = 165683
threes = 166465
fours = 166924
fives = 166912
sixes = 167409
chr@chr:~$
Kode: Vælg alt
chr@chr:~$ /home/chr/die.sh
ones = 1666619
twos = 1665531
threes = 1667795
fours = 1667018
fives = 1666097
sixes = 1666940
chr@chr:~$
AJenbo skrev:Pas på du ikke forveksler statistisk med tilfældig.
Kode: Vælg alt
#!/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 # Number of times the dice have been cast.
ones=0 # Must initialize counts to zero,
twos=0 #+ since an uninitialized variable is null, NOT zero.
threes=0
fours=0
fives=0
sixes=0
print_result ()
{
echo
echo "ones = $ones"
echo "twos = $twos"
echo "threes = $threes"
echo "fours = $fours"
echo "fives = $fives"
echo "sixes = $sixes"
echo
}
update_count()
{
case "$1" in
0) ((ones++));; # Since a die has no "zero", this corresponds to 1.
1) ((twos++));; # And this to 2.
2) ((threes++));; # And so forth.
3) ((fours++));;
4) ((fives++));;
5) ((sixes++));;
esac
}
echo
while [ "$throw" -lt "$MAXTHROWS" ]
do
let "die1 = RANDOM % $PIPS"
update_count $die1
let "throw += 1"
done
print_result
exit $?
# The scores should distribute evenly, assuming RANDOM is 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."
Kode: Vælg alt
chr@chr:~$ /home/chr/die.sh
ones = 16673196
twos = 16666886
threes = 16663910
fours = 16666540
fives = 16668407
sixes = 16661061
chr@chr:~$
Kode: Vælg alt
chr@chr:~$ /home/chr/die.sh
ones = 166678171
twos = 166690535
threes = 166667649
fours = 166667478
fives = 166660405
sixes = 166635762
chr@chr:~$
Kode: Vælg alt
chr@chr:~$ /home/chr/die.sh
ones = 1666891
twos = 1668247
threes = 1665170
fours = 1666624
fives = 1667448
sixes = 1665620
chr@chr:~$
AJenbo skrev:Tja det viser vist blot at dette ikke er en god måde at se om resultatet er tilfældigt