scripts

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!

5 Comments

  • 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! :)

    Pera.

  • 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 random.org:

    #!/bin/ksh
    while true;do
    dice=$( wget -q -O - "http://random.org/integers/?num=1&min=1&max=6&col=5&base=10&format=plain&rnd=new" )
    echo $dice
    read
    clear
    done

  • 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.
    Thanks!

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