We often consider the terminal as one thing purely purposeful—tweaking Linux settings, putting in updates, or working instructions for work. And for most individuals, that’s the place it ends. However the terminal may be extra versatile than you would possibly count on. One surprisingly enjoyable use case? Rolling cube. Sure, your Linux terminal can double as a digital cube curler.

Why Roll Cube within the Terminal?

Now, you would possibly surprise, why trouble rolling cube within the terminal after we have already got bodily cube? Truthful query! Cube are certainly the heartbeat of board games like Monopoly, Catan, and Dungeons & Dragons. They determine victories, defeats, and typically even spark heated arguments.

However whereas actual cube have their attraction, in addition they include loads of complications. They roll underneath the couch, disappear simply whenever you want them, or bounce throughout the room mid-game. And low-cost units may be unbalanced, which may skew outcomes and get you into an unfortunate streak.

Additionally, in RPGs, the place you would possibly roll a fistful of cube directly, retaining monitor of the numbers could be a actual chore; as a substitute of having fun with the sport, everybody is targeted on working numbers of their head to make sure the validity of the calculation. After which, specialty cube, like the huge d100, are even worse. They’re cumbersome, roll ceaselessly, and will not settle onto a single face very simply. And in addition, it is fairly tough to learn.

The terminal neatly avoids all of that. Want a twenty-sided die? Or perhaps a d100? Your terminal can roll any die immediately, irrespective of how uncommon. And also you’ll by no means need to go underneath the couch, by no means fear about bias, and at all times have entry, whether or not you’re at dwelling, touring, or at work. Simply open the terminal and also you’re good to go.

Aside from practicality, rolling cube in a terminal may also be a enjoyable trick to point out off to your folks. No web sites, no apps, only a fast command, and your folks will probably be in awe. Even higher, it’s a playful, low-pressure approach to get comfy with the terminal whereas nonetheless doing one thing sensible but enjoyable. Who is aware of, you would possibly even create your individual terminal-based recreation!

Technique 1: Putting in rolldice on Linux

Let’s get going with rolling our cube within the terminal. For the primary technique, we are going to obtain a software referred to as rolldice. It helps the usual RPG cube notation (NdM, like 3d6+2) and makes it simple to roll single cube, a number of cube, and even apply modifiers.

Putting in rolldice

The set up will rely upon the Linux distro you might be working.

For Debian/Ubuntu

        sudo apt set up rolldice
    

In case you have Fedora/RHEL, set up rolldice by working:

        sudo dnf set up rolldice
    

For Arch/Manjaro, rolldice is out there within the Arch Consumer Repository (AUR). You’ll be able to set up it utilizing an AUR helper akin to yay.

        yay -S rolldice
    

As soon as the bundle is put in, be certain that it is working. Run this command:

        rolldice 
    

The output would be the model of rolldice in your system. This confirms the bundle is efficiently put in. In the event you get an error, strive reinstalling rolldice.

Utilizing rolldice

Utilizing rolldice is straightforward. The syntax follows NdM notation. The place N is the variety of cube you wish to roll. M is the variety of sides on both sides.

For the usual six-sided die, the notation will probably be 1d6, which suggests roll one 6-sided die.

Now, if you’re rolling three 6-sided cube, the notation will probably be 3d6. Now, guess what 3d20 means? It means you might be rolling three 20-sided cube.

Roll a Single Die

Now let the terminal roll our cube. To roll a normal six-sided die, kind in your terminal:

        rolldice 1d6
    
Linux terminal showing rolldice tool to roll dice using terminal

Roll Bigger Die

Now, what when you needed to roll one thing past a easy 1d6? Particularly in your tabletop video games like Dungeons & Dragons, the place you roll 20, 30, and even 90-sided cube? Simply specify no matter quantity you want. To roll a 20-sided die, kind in your terminal:

        rolldice 1d20
    
Linux terminal showing rolldice tool to roll dice using terminal

Roll A number of Cube at As soon as

Rolling a single die was fairly simple, proper? So now let’s roll a number of cube. For instance, to roll three six-sided cube, kind in your terminal:

        rolldice 3d6
    
Linux terminal showing rolldice tool to roll dice using terminal

Making use of Modifiers

In lots of RPGs, cube rolls aren’t at all times taken “as is.” You usually add modifiers or bonuses for sturdy talents, penalties for powerful conditions, or ability checks that offer you an edge. For this, rolldice will deal with the maths for you. No extra juggling numbers in your head.

For instance, when you’re rolling 4 8-sided cube and want so as to add +2 (say, for a personality’s power bonus), you’d run:

        rolldice 4d8+2
    
Linux terminal showing rolldice tool to roll dice using terminal

So, let’s assume you roll 4 8-sided cube, getting numbers (3, 5, 7, and a pair of). This totals to 17, you then add 2 for character power, which supplies you 19. Neat and clear, all accomplished by your ever-useful Linux terminal.

In video games like Dungeons & Dragons, nearly each roll has some type of modifier. So subsequent time, as a substitute of rolling the cube after which counting the numbers manually, the place you might be prone to make calculation errors underneath the warmth of the sport, let the terminal do the work whereas everybody can concentrate on the precise factor: play the sport.

Roll A number of Units Collectively

Now, what if you wish to roll a number of totally different cube units directly? Easy. Simply separate them with areas.

        rolldice 3d6 1d20 2d4+1
    
Linux terminal showing rolldice tool to roll dice using terminal

Technique 2: Utilizing the shuf Command

Now, when you don’t wish to set up an additional bundle like rolldice, otherwise you’re offline at your forest home with nothing however a terminal. You’ll be able to nonetheless roll cube utilizing instruments which are already constructed into most Linux methods. One of many easiest is the shuf command, which is a part of GNU coreutils.

The shuf command is often used to shuffle enter traces or choose random values from a spread, nevertheless it additionally works completely for simulating cube rolls.

First, let’s have a look at how the shuf command works by shuffling some fruits:

        echo -e "applenbananancherry" | shuf
Linux terminal showing shuf tool for randomizing inputs

It shuffled the enter traces (every fruit is a line) and printed them in a random order. Now, whenever you use shuf with the -i choice, you don’t have to supply your individual checklist; it generates a spread of numbers for you. That’s how we’re going to use the shuf command to roll cube.

Roll a Six-Sided Die

To roll a single six-sided die, kind the next in your terminal:

        shuf -i 1-6 -n 1
    
Linux terminal showing shuf tool for rolling dice

To know higher, let’s dig into the syntax:

  • -i 1-6 units the vary (numbers 1 via 6).
  • -n 1 tells shuf to output only one quantity.

Roll A number of Cube

To roll a number of cube directly, merely improve the -n worth. For instance, to roll three six-sided cube:

        shuf -i 1-6 -n 3
    
Linux terminal showing shuf tool for rolling multiple dice

Every roll will seem by itself line. Not like rolldice, shuf doesn’t calculate the entire routinely, so you’ll usually have so as to add the numbers your self. We are able to sum the numbers immediately contained in the terminal.

        echo $((2+5+6))
    
Adding numbers in Linux terminal

Roll Bigger Die

To roll a d20 or a d100? Simply change the vary:

        shuf -i 1-20 -n 1
    
        shuf -i 1-100 -n 1
    
Linux terminal showing shuf tool for rolling dice with 100 sides

Whereas shuf is useful, it’s extra restricted than rolldice. For one, it doesn’t assist RPG notation like 3d6+2, and also you’ll must calculate sums your self. That mentioned, when you simply need a fast, no-frills approach to roll cube on Linux, shuf will get the job accomplished properly.

Technique 3: Utilizing Bash With $RANDOM

One other approach to roll cube with out putting in something is to make use of the $RANDOM variable constructed into Bash. Every time you reference $RANDOM, it generates a pseudo-random integer between 0 and 32767. With slightly math, you’ll be able to convert that into any type of cube roll.

Roll a Six-Sided Die

To roll a normal six-sided die (d6), kind:

        echo $(( (RANDOM % 6) + 1 ))
    
Linux terminal showing $RANDOM variable being used to roll dice

Right here’s what’s taking place:

  • The % operator is the modulo operator, which supplies the rest after division.
  • RANDOM % 6 takes the random quantity (0–32767) and divides it by 6, giving a the rest between 0 and 5.
  • Including +1 shifts the vary to 1–6, similar to an actual die.

Roll a Twenty-Sided Die

Rolling a twenty-sided die (d20) works the identical method—simply change the quantity:

        echo $(( (RANDOM % 20) + 1 ))
    
A Linux terminal showing $RANDOM variable being used to roll dice

Roll A number of Cube

Rolling a number of cube is a little more advanced, nevertheless it’s an effective way to follow loops in bash—and impress your folks together with your Linux expertise! Right here’s how one can roll 3 six-sided cube (3d6). There are two approaches: line-by-line and a one-liner.

The primary strategy is line-by-line. I like to recommend that you simply observe this strategy. The reason being that it is simpler; secondly, the code will get logically damaged down into its personal line, making it a lot simpler to grasp what is going on on. So, simply kind every a part of the loop by itself line. The terminal gives you a continuation immediate (>) till the loop is full, as you’ll be able to see under:

Linux terminal showing $RANDOM variable being used to roll multiple dice

The traces you must kind are as follows:

        sum=0

for i in 1 2 3; do

roll=$((RANDOM % 6 + 1))

echo "Roll $i: $roll"

sum=$((sum + roll))

accomplished

echo "Complete: $sum"

Linux terminal showing $RANDOM variable being used to roll multiple dice using loop

The second strategy is a one-liner. It is a compact model to do the identical factor. When you’re comfy with the syntax, you’ll be able to write your complete loop in a single line utilizing semicolons (;) to separate instructions:

sum=0; for i in 1 2 3; do roll=$((RANDOM % 6 + 1)); echo "Roll $i: $roll"; sum=$((sum + roll)); accomplished; echo "Complete: $sum"
Linux terminal showing $RANDOM in bash being used to roll dice using loop in

Press Enter, and the loop runs unexpectedly. This model is shorter, however more durable to debug if one thing goes fallacious.


Even when you’re new to the terminal, these instructions make rolling cube simple, enjoyable, and dependable. Excellent for recreation night time with pals. No extra looking for misplaced cube underneath the sofa; your terminal has you lined.

Use rolldice for full RPG options like modifiers and totals. That is the best approach to roll a die within the terminal. Use shuf for fast, offline random rolls, and at last, you should use $RANDOM in bash for a light-weight, no-install choice plus the showoff quirks!


Source link