Randoword.sh

Posted on October 30, 2006

I’ve had variations of this script kicking around in my .bash_history for quite some time now. The first version of it merely printed a random word from my dictionary file at a rate of one per second. This served as a source of inspiration/entropy for irc conversations and Flickr titles. Today, I decided to extend this one-liner to generate eight-words-at-a-time at a variable rate and jigger the output into an irssi process to amuse the good netizens. Here is the implementation for randoword.sh.


#!/bin/sh
i=0
while true; do
  let i=$i+1
  dictionary="/usr/share/dict/linux.words"
  dl=($(wc -l $dictionary))
  echo -n "randowords #$i: "
  for j in $( seq 1 8 ); do
    head -n$(($(head -c4 /dev/urandom | od -An -tu4) % $dl))\
    $dictionary| tail -n1
  done | xargs
  sleep $((RANDOM % 3600))
done

Update: I never really understood how to use random numbers within a bash script without invoking perl (or equivalent). In light of _fool’s comment, I rewrote my script using only bash and POSIX commands. I had to break-out /dev/urandom and od because bash’s $RANDOM variable only produces random values up to 32767. Technically, it’d be a whole lot faster for a C program to use a pointer to jump around randomly in the dictionary, but that seems too much like work.

Update 2: The seq command probably isn’t POSIX.

Update 3: In Ruby: ruby -e ' $w=STDIN.readlines; 8.times{print "#{$w[rand(483523)].chomp} "}’ < /usr/share/dict/linux.words

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. jtax Thu, 02 Nov 2006 13:05:00 MST

    ruby is soooo cool!

  2. Administrator Thu, 02 Nov 2006 15:47:22 MST

    OMGRUBYR0X0Rz!111l11LOLLOLz0r!

Comments

Randoword.sh

Posted on October 30, 2006

I’ve had variations of this script kicking around in my .bash_history for quite some time now. The first version of it merely printed a random word from my dictionary file at a rate of one per second. This served as a source of inspiration/entropy for irc conversations and Flickr titles. Today, I decided to extend this one-liner to generate eight-words-at-a-time at a variable rate and jigger the output into an irssi process to amuse the good netizens. Here is the implementation for randoword.sh.


#!/bin/sh
i=0
while true; do
  let i=$i+1
  dictionary="/usr/share/dict/linux.words"
  dl=($(wc -l $dictionary))
  echo -n "randowords #$i: "
  for j in $( seq 1 8 ); do
    head -n$(($(head -c4 /dev/urandom | od -An -tu4) % $dl)) $dictionary| tail -n1
  done | xargs
  sleep $((RANDOM % 3600))
done

Update: I never really understood how to use random numbers within a bash script without invoking perl (or equivalent). In light of fool’s comment, I rewrote my script using only bash and POSIX commands. I had to break-out /dev/urandom and od because bash’s $RANDOM variable only produces random values up to 32767. Technically, it’d be a whole lot faster for a C program to use a pointer to jump around randomly in the dictionary, but that seems too much like work.

Update 2: The seq command probably isn’t POSIX.

Update 3: In Ruby: ruby -e ' $w=STDIN.readlines; 8.times{print "#{$w[rand(483523)].chomp} "}’ < /usr/share/dict/linux.words

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

Comments