OP
CrazedNerd
Guest
So today I went ahead and finished my thought experiment, of how i could use bash to make a fairly random choice...here is my "proof of concept" for my random website selector idea as a psuedo random number generator.
So this script works so that each time a 0,1,2,3,4,5,6,7,8,9 is pulled out of the system clock (using sed to choose the last digit), then it gets stored in a variable value so that we can see the distribution of numbers chosen.
The results of running this a few times in sequence, which I put into a text file and labeled according to nth time I ran the script, shows that with each time you run the script, it starts on a different number and has an equal and predictable distribution. This is just a psuedo random number generator, but it has the "truly random" aspect in the sense that it can't favor one particular number more so than the others, even though anyone looking at the system clock would know the exact outcome of the generator...so you definetly couldn't use this for "cyptographic security", because anyone could figure out that this is based on a system clock, even if they couldn't see the algorithm in bare form:
SO IN OTHER WORDS, the first time this thing was executed, 9 was the first number selected by sed, then 3, then 5. You could use this to select possible options in a script randomly, but it predictably counts from 0 to 9 by the second.
I think one way you could turn this into something truly random would be to have a clock that is broken down into nano or milliseconds, because both of those time units are imperceptible to all humans. You would never be able to pinpoint the exact millisecond of a day...no matter how hard you try.
So this script works so that each time a 0,1,2,3,4,5,6,7,8,9 is pulled out of the system clock (using sed to choose the last digit), then it gets stored in a variable value so that we can see the distribution of numbers chosen.
Code:
#!/bin/bash
#simple PRNG in bash
a=0
b=0
c=0
d=0
e=0
f=0
g=0
h=0
i=0
j=0
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
for ((k=0; k<=30; k++)); do
if [ "$digit" = 0 ]; then
sleep 1
((a++))
clear
echo "Occurences of 0: $a"
echo "Occurences of 1: $b"
echo "Occurences of 2: $c"
echo "Occurences of 3: $d"
echo "Occurences of 4: $e"
echo "Occurences of 5: $f"
echo "Occurences of 6: $g"
echo "Occurences of 7: $h"
echo "Occurences of 8: $i"
echo "Occurences of 9: $j"
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
elif [ "$digit" = 1 ]; then
sleep 1
((b++))
clear
echo "Occurences of 0: $a"
echo "Occurences of 1: $b"
echo "Occurences of 2: $c"
echo "Occurences of 3: $d"
echo "Occurences of 4: $e"
echo "Occurences of 5: $f"
echo "Occurences of 6: $g"
echo "Occurences of 7: $h"
echo "Occurences of 8: $i"
echo "Occurences of 9: $j"
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
elif [ "$digit" = 2 ]; then
sleep 1
((c++))
clear
echo "Occurences of 0: $a"
echo "Occurences of 1: $b"
echo "Occurences of 2: $c"
echo "Occurences of 3: $d"
echo "Occurences of 4: $e"
echo "Occurences of 5: $f"
echo "Occurences of 6: $g"
echo "Occurences of 7: $h"
echo "Occurences of 8: $i"
echo "Occurences of 9: $j"
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
elif [ "$digit" = 3 ]; then
sleep 1
((d++))
clear
echo "Occurences of 0: $a"
echo "Occurences of 1: $b"
echo "Occurences of 2: $c"
echo "Occurences of 3: $d"
echo "Occurences of 4: $e"
echo "Occurences of 5: $f"
echo "Occurences of 6: $g"
echo "Occurences of 7: $h"
echo "Occurences of 8: $i"
echo "Occurences of 9: $j"
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
elif [ "$digit" = 4 ]; then
sleep 1
((e++))
clear
echo "Occurences of 0: $a"
echo "Occurences of 1: $b"
echo "Occurences of 2: $c"
echo "Occurences of 3: $d"
echo "Occurences of 4: $e"
echo "Occurences of 5: $f"
echo "Occurences of 6: $g"
echo "Occurences of 7: $h"
echo "Occurences of 8: $i"
echo "Occurences of 9: $j"
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
elif [ "$digit" = 5 ]; then
sleep 1
((f++))
clear
echo "Occurences of 0: $a"
echo "Occurences of 1: $b"
echo "Occurences of 2: $c"
echo "Occurences of 3: $d"
echo "Occurences of 4: $e"
echo "Occurences of 5: $f"
echo "Occurences of 6: $g"
echo "Occurences of 7: $h"
echo "Occurences of 8: $i"
echo "Occurences of 9: $j"
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
elif [ "$digit" = 6 ]; then
sleep 1
((g++))
clear
echo "Occurences of 0: $a"
echo "Occurences of 1: $b"
echo "Occurences of 2: $c"
echo "Occurences of 3: $d"
echo "Occurences of 4: $e"
echo "Occurences of 5: $f"
echo "Occurences of 6: $g"
echo "Occurences of 7: $h"
echo "Occurences of 8: $i"
echo "Occurences of 9: $j"
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
elif [ "$digit" = 7 ]; then
sleep 1
((h++))
clear
echo "Occurences of 0: $a"
echo "Occurences of 1: $b"
echo "Occurences of 2: $c"
echo "Occurences of 3: $d"
echo "Occurences of 4: $e"
echo "Occurences of 5: $f"
echo "Occurences of 6: $g"
echo "Occurences of 7: $h"
echo "Occurences of 8: $i"
echo "Occurences of 9: $j"
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
elif [ "$digit" = 8 ]; then
sleep 1
((i++))
clear
echo "Occurences of 0: $a"
echo "Occurences of 1: $b"
echo "Occurences of 2: $c"
echo "Occurences of 3: $d"
echo "Occurences of 4: $e"
echo "Occurences of 5: $f"
echo "Occurences of 6: $g"
echo "Occurences of 7: $h"
echo "Occurences of 8: $i"
echo "Occurences of 9: $j"
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
else
sleep 1
((j++))
clear
echo "Occurences of 0: $a"
echo "Occurences of 1: $b"
echo "Occurences of 2: $c"
echo "Occurences of 3: $d"
echo "Occurences of 4: $e"
echo "Occurences of 5: $f"
echo "Occurences of 6: $g"
echo "Occurences of 7: $h"
echo "Occurences of 8: $i"
echo "Occurences of 9: $j"
now=$(date +%s)
digit=$(echo $now | sed 's/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)/\4/g')
fi
done
The results of running this a few times in sequence, which I put into a text file and labeled according to nth time I ran the script, shows that with each time you run the script, it starts on a different number and has an equal and predictable distribution. This is just a psuedo random number generator, but it has the "truly random" aspect in the sense that it can't favor one particular number more so than the others, even though anyone looking at the system clock would know the exact outcome of the generator...so you definetly couldn't use this for "cyptographic security", because anyone could figure out that this is based on a system clock, even if they couldn't see the algorithm in bare form:
Code:
TRIAL 1:
Occurences of 0: 3
Occurences of 1: 3
Occurences of 2: 3
Occurences of 3: 3
Occurences of 4: 3
Occurences of 5: 3
Occurences of 6: 3
Occurences of 7: 3
Occurences of 8: 3
Occurences of 9: 4
TRIAL 2:
Occurences of 0: 3
Occurences of 1: 3
Occurences of 2: 3
Occurences of 3: 4
Occurences of 4: 3
Occurences of 5: 3
Occurences of 6: 3
Occurences of 7: 3
Occurences of 8: 3
Occurences of 9: 3
TRIAL 3:
Occurences of 0: 3
Occurences of 1: 3
Occurences of 2: 3
Occurences of 3: 3
Occurences of 4: 3
Occurences of 5: 4
Occurences of 6: 3
Occurences of 7: 3
Occurences of 8: 3
Occurences of 9: 3
SO IN OTHER WORDS, the first time this thing was executed, 9 was the first number selected by sed, then 3, then 5. You could use this to select possible options in a script randomly, but it predictably counts from 0 to 9 by the second.
I think one way you could turn this into something truly random would be to have a clock that is broken down into nano or milliseconds, because both of those time units are imperceptible to all humans. You would never be able to pinpoint the exact millisecond of a day...no matter how hard you try.