Roll the dice

Not a command, but a fun thing anyway. What if you need a dice (as I needed the other day while trying to play a game with my friends) and you don’t have one?
Bash can do it:

$ while true;do let dice="$RANDOM % 6"; let dice=$dice+1;echo $dice;read;clear;done

Though it’s a long line, this is just an infinite loop in which we use the $RANDOM bash variable which generates a random number between 0 and 32767. After that, we divide it by 6 and take the remainder (so we have a number between 0 and 5) and add 1 to shift the interval so we get numbers between 1 and 6.

Have fun!


  • On 07.19.08 Pera said:

    Wrong: that is not a true dice because 32768 is not divisible by 6!!! Then you have mmmhhh: 0 and 1 (i.e. 1 and 2) have 1 possibility more than the others of showing up: 0->5462, 1->5462, 2->5461, 3->5461, 4->5461, 5->5461, 6->5461.

    I got you! :)


  • On 07.21.08 n0str0m0 said:

    Hey! I’m an engineer. Approximation is my business XD

  • On 03.24.09 John Baber said:

    If you want truly random numbers 1-6 without having to think about it, you can always call on

    while true;do
    dice=$( wget -q -O - "" )
    echo $dice

  • On 03.24.09 John Baber said:

    Hey, my “code” tag seems to have ended after the first line…

  • On 03.24.09 n0str0m0 said:

    Very interesting, I didn’t know that service.

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields