Nushell Niceties: Rolling Dice
Nushell has some nice built-in commands to get randomized data. The random
command can be used to get random numbers, strings, and more. You can use the dice
subcommand to get random numbers between 1 and 6. The command returns a list of integers. With the option --dice
you can specify how many times to throw the dice. By default the dice has 6 sides, but you can use the option --sides
to change that. You could roll a dice with 2 sides, like flipping a coin, or roll a dice with 10 sides.
In the following examples you can see how to use the random dice
command. With the standard random dice
command you see a table with one row. The first column contains the list index and the second column contains the result of the dice throw:
> random dice # throw a standard dice
╭───┬───╮
│ 0 │ 2 │
╰───┴───╯
To get the result of the dice throw you can use the first
command to get the value of the second column:
> random dice | first # get the result of the dice throw
3
In the following example you can see how to throw a dice 5 times:
> random dice --dice 5 # throw a standard dice 5 times
╭───┬───╮
│ 0 │ 3 │
│ 1 │ 2 │
│ 2 │ 2 │
│ 3 │ 6 │
│ 4 │ 3 │
╰───┴───╯
To get a dice with only two sides you can use the --sides 2
option:
> random dice --sides 2 # dice with 2 sides, like flipping a coin
╭───┬───╮
│ 0 │ 1 │
╰───┴───╯
In the next examples the options --sides
and --dice
are combined:
> random dice --sides 2 --dice 5 # dice with 2 sides, like flipping a coin
╭───┬───╮
│ 0 │ 2 │
│ 1 │ 1 │
│ 2 │ 2 │
│ 3 │ 1 │
│ 4 │ 2 │
╰───┴───╯
> random dice --sides 10 --dice 5 # dice with 10 sides
╭───┬───╮
│ 0 │ 3 │
│ 1 │ 4 │
│ 2 │ 6 │
│ 3 │ 3 │
│ 4 │ 8 │
╰───┴───╯
Finally a small example where the result of the dice throw is transformed into a dice icon using the command char --unicode
:
> random dice --dice 5 |
each { char --unicode ($in + 2679 | into string) } # transform into dice icons
╭───┬───╮
│ 0 │ ⚀ │
│ 1 │ ⚅ │
│ 2 │ ⚄ │
│ 3 │ ⚂ │
│ 4 │ ⚁ │
╰───┴───╯
Written with Nushell 0.104.0.