Random word from a flat text file

Hello,
I need to take a random word from a flat text file with words in it seperated by spaces.

The code I am using, always gives me the first word. Can anyone please shed some light on this. Here's my code.

Thanks

echo table roof ceiling jar computer monitor keyboard carpet > wordfile

            file_name=wordfile  \# $file_name is the name of file that you select a random word
            num_words=\`wc -w $file_name | cut -d " " -f 1\`
            rand_word=\`expr "$RANDOM" % $num_words \+ 1\`
                    i=0
                            for word in \`cat $file_name\`
                            do
                            i=\`expr $i \+ 1\`
                                    if [ $i = $rand_word ]
                                    then
                                             echo "$word" > hidd
                                    fi

where is $RANDOM defined?

How do i define $RANDOM ?

try this

file_name=wordfile

num_words=`wc -w < $file_name`

rand_word=`expr "$RANDOM" % $num_words + 1`

cat $file_name | tr "\n" " " | awk -v randword=$rand_word '{print $randword}'

if you have Python, here's an easy way to do it

#!/usr/bin/env python
import random
data=open("file").read().split()
print random.sample(data,1)[0]

$RANDOM doesn't need to be defined. It's a shell function, like $PWD.
I didn't go through the OP script, but here's a shorter version using arrays in korn shell:

 # cat random_script.ksh
set -A WORDS $(cat wordfile)
RAN_NUM=$(echo ${WORDS[@]} | wc -w)
echo ${WORDS[$(expr "$RANDOM" % ${RAN_NUM})]}

There's probably a shorter way using AWK.