C
CrazedNerd
Guest
Hello everyone, I'm writing this not really as a guide, but thinking out loud...
So you might at one point want a computer to randomly generate a number for you. Maybe you are designing a game of chance, or maybe you and your friends are having trouble making a final decision on something that's not very important. Maybe you need to sacrifice someone to the gods yet be impartial/detached in your decision.
Well, that last part is a joke...but in reality, RNGs are pretty fun. They exist all over the internet, but you can also generate random numbers from the command line line this:
This one generates a large range of numbers, starting from 0 and ending at 32,767. That's pretty awesome, how many of you have counted to 32,768 ?! Someone certainly has, because it's not that big of number in the grand scheme of things, but it's plenty huge for adding some chance to a lot of different possible things.
You can also use your system's clock (probably like $RANDOM does) to add an element of chance to flow control in a script you write with bash. In the fallowing small example, you use modulus to simulate a coin flip with odd and even numbers from your system clock's seconds:
The system clock at this point has over a billion seconds, because it has been increasing since the "dawn of unix" on 01/01/1971. Obviously echoing $RANDOM gives you a better selection of options, but unfortunately using $RANDOM in a script results in the first number chosen staying the same number every other time you execute the script, which completely defeats the purpose...but rest assured, there's a lot of predictability in that, because the number of place values won't increase in that number for over 9 years. So, one could make a better RNG using some more complex math than listed above.
So you might at one point want a computer to randomly generate a number for you. Maybe you are designing a game of chance, or maybe you and your friends are having trouble making a final decision on something that's not very important. Maybe you need to sacrifice someone to the gods yet be impartial/detached in your decision.
Well, that last part is a joke...but in reality, RNGs are pretty fun. They exist all over the internet, but you can also generate random numbers from the command line line this:
Code:
echo $RANDOM
This one generates a large range of numbers, starting from 0 and ending at 32,767. That's pretty awesome, how many of you have counted to 32,768 ?! Someone certainly has, because it's not that big of number in the grand scheme of things, but it's plenty huge for adding some chance to a lot of different possible things.
You can also use your system's clock (probably like $RANDOM does) to add an element of chance to flow control in a script you write with bash. In the fallowing small example, you use modulus to simulate a coin flip with odd and even numbers from your system clock's seconds:
Code:
now=$(date +%s)
flip=$((now % 2))
if [ $flip = 0 ]; then
echo HEADS
else
echo TAILS
fi
The system clock at this point has over a billion seconds, because it has been increasing since the "dawn of unix" on 01/01/1971. Obviously echoing $RANDOM gives you a better selection of options, but unfortunately using $RANDOM in a script results in the first number chosen staying the same number every other time you execute the script, which completely defeats the purpose...but rest assured, there's a lot of predictability in that, because the number of place values won't increase in that number for over 9 years. So, one could make a better RNG using some more complex math than listed above.