Today’s article is going to be more or less an intellectual article, as there isn’t much need for folks to generate random numbers. It’s just a fun article that will show you a neat trick you can perform in the Linux terminal. If you’ve ever wanted to generate random numbers, this just might be the article for you!
First, let’s say a few things…
One, this is not true random. True random is something you’re not going to get with a computer. While Bell’s Theorem insists that true random exists in the universe (such as the rate of decay in a radioactive isotope), it’s really hard to achieve randomness.
Instead, we rely on pseudorandom – and we rely on it a great deal. Cryptography relies on it as much as a security certificate. They all deal in randomness and there are different degrees of random. What’s important is predictability.
While I’d love to turn this into a giant mathematical discussion, let’s just agree that you’re never going to get true randomness from your computer. But, we can also agree that you’ll get results that are random enough for our purposes.
Even with security certificates, we do not have true random as our backing. However, it’s random enough for advanced certificates to be reasonably unbreakable at this time. Trust me, I’d love to make this a geeky discussion, but for our purposes just accept that there’s a spectrum between predictable outcomes and true random. Everything you’re ever going to approach is going to be on that spectrum.
If you want true random, there’s random.org which claims to generate truly random numbers based on atmospheric noise. (Basically, they’re using what you’d think of as static as a source for randomness.) I am going to highly suggest those interested view the following article from the random.org website:
Introduction to Randomness and Random Numbers
That’s going to be complicated for some of my viewers, but I have high hopes that some of my viewers will be appropriately ‘geeked out’ by the discussion. It takes true Philosophers of Mathematics to grasp the intricacies (and implications) of random, pseudorandom, and true random.
There are those (including myself) that are a bit skeptical of the concept of true random – but those, like me, accept the aforementioned Bell’s Theorem because it has been thoroughly tested and stands as being the most acceptable answer to our observations. We call this science and I’m a firm believer in the scientific method.
So, at this time, we have ‘conclusive’ evidence that true random exists in the universe. It’s just REALLY complicated and takes many pages of math to describe. After all, there are things like this:
Random numbers certified by Bell’s theorem (Nature)
And, for the truly daring, I encourage you to view the following Wikipedia article that delves into the theorem itself:
Trust me, all sides of this have been thoroughly discussed at the Muddy Charles Pub. People far smarter than myself have hashed this out over many pints, both from a mathematical and scientific viewpoint and from a philosophical viewpoint. After all, we humans have a nasty habit of claiming something is random while the reality is that our understanding is just incomplete. As such, randomness is a tricky subject.
We’ll be using the ‘shuf’ command in this exercise. It’s installed by default and uses both /dev/random and /dev/urandom for its output. The command is simple enough to use and suitable for making your own random dice generator for your next Dungeons & Dragons game. Given manufacturing discrepancies, it’s probably more random than actual dice.
The ‘shuf’ command describes itself as this:
shuf – generate random permutations
Every distro will likely have this installed by default. If it’s not installed in your distro (check shuf --version
to ensure it shuf is installed) you’ll need to install it for the sake of this exercise in generating random numbers.
Other than that, you won’t need anything but an open terminal…
Generate Random Numbers:
If you can’t tell, this is a subject I’m passionate about. Many of you will know that my background is being a mathematician, albeit retired. To me, the subject of randomness (and I hope to have given you a taste) is fascinating. This won’t be true random, but it will be fun. So, let’s just press
With your terminal open, the syntax of the command is as follows:
1 | shuf -i <low_number>-<high_number> |
If you execute that command, it will list every number between those two values (lowest number and highest number) in random order. For example:
1 2 3 4 5 6 7 8 9 10 11 | kgiii@kgiii:~$ shuf -i 1-10 6 10 2 4 9 3 5 7 1 8 |
If you only want a few random numbers, the syntax would look like this:
1 | shuf -i <low_number>-<high_number> -n <number_of_results> |
For example, you might want to get three random numbers between 1 and 20. That’s easily done. You’d simply use this command:
1 | shuf -i 1-20 -n 3 |
An example of that output might be:
1 2 3 4 | kgiii@kgiii:~$ shuf -i 1-20 -n 3 1 16 20 |
So, if you wanted to replicate a 3d6 roll (three die six) with a single output would be done like this:
1 | shuf -1 3-18 -n 1 |
Of course, if you wanted to be fancy for your D&D-playing friends, you could expand the command. (I rolled an 18 on my first try!) See this:
1 2 3 | kgiii@kgiii:~$ echo "You rolled 3d6 and the output is:" && shuf -i 3-18 -n 1 You rolled 3d6 and the output is: 18 |
See? Good times! If you want to generate random numbers (for a fun definition of random, and a truly useful random) you can do so quickly and easily from right there in the Linux terminal.
Closure:
Trust me, this is the short version… I wanted to write more about randomness. I have a love for mathematics that I suspect isn’t shared by many. I know it isn’t shared by many – and there’s a reason for that but this isn’t the site or topic to get into that. (Hint: It has to do with how mathematics is taught at an early age.)
So, for me, random is a special subject – much like infinity. These are concepts to be mulled over and considered. They are ideas that we’ve taken and made real. Even if they didn’t exist (and infinity still doesn’t exist), we’d use them as concepts, which is good enough for the real world.
These are concepts that are as old as mathematical and logical thinking has existed, as math is just logicism at its core. They’re like a fine wine, sipped at and quantified – but never quite fully understood. And that’s okay. For us, we sip and dine at the table of logic, using what tools we have to appreciate the experience.
Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.