Created
March 5, 2011 12:13
-
-
Save keesj/856314 to your computer and use it in GitHub Desktop.
using RANDOM + MOD is not a good Random nummber generator
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
# Prove using RANDOM + MOD is not a good Random nummber generator | |
# | |
# CATEGORIES == the amount of "winners" in the experiment | |
# @author freakingtux | |
CATEGORIES=707 | |
EXPERIMENTS=100000 | |
if ! which gnuplot 2>&1 > /dev/null | |
then | |
echo "This script requires gnuplot" | |
exit 1 | |
fi | |
# problem # $RANDOM returns a different random integer at each invocation. | |
# Nominal range: 0 - 32767 (signed 16-bit integer). | |
# if you assume that every number is draw as often as the other you will find | |
# that the first people will be "more lucky" as there is a bigger chance the number | |
# end with a value lower then MAX % CATEGORIES in this case 32767 % 707 | |
# the first 245 contestants have a bigger win factor ! | |
COUNT=0 | |
while [ $COUNT -lt $CATEGORIES ] | |
do | |
data[$draw]=0 | |
COUNT=$[$COUNT +1] | |
done | |
COUNT=0 | |
while [ $COUNT -lt 32767 ] | |
do | |
draw=$[ $[ $COUNT % $CATEGORIES ]] | |
data[$draw]=$[${data[$draw]} +1] | |
COUNT=$[$COUNT +1] | |
done | |
cat <<EINDE | |
echo This shows the people at the top of the list have more chance of winning. | |
echo because theire number will be drawn more often. | |
EINDE | |
( | |
COUNT=0 | |
while [ $COUNT -lt $CATEGORIES ] | |
do | |
echo $COUNT ${data[$COUNT]} | |
COUNT=$[$COUNT +1] | |
done ) | gnuplot -e "set term dumb ; plot \"-\" with steps " | |
#NOTE if you remove this next line somehow (I don't know why | |
#the last value | |
#########echo REMOVE ME and see the last value | |
# Perform the real exeriment | |
COUNT=0 | |
while [ $COUNT -lt $CATEGORIES ] | |
do | |
data[$draw]=0 | |
COUNT=$[$COUNT +1] | |
done | |
COUNT=0 | |
while [ $COUNT -lt $EXPERIMENTS ] | |
do | |
draw=$[ $[ RANDOM % $CATEGORIES ]] | |
data[$draw]=$[${data[$draw]} +1] | |
COUNT=$[$COUNT +1] | |
done | |
( | |
COUNT=0 | |
while [ $COUNT -lt $CATEGORIES ] | |
do | |
echo $COUNT ${data[$COUNT]} | |
COUNT=$[$COUNT +1] | |
done ) | gnuplot -e "set term dumb ; plot \"-\" with steps " |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment