Friday, May 16, 2008

Generate random words in linux


/usr/share/dict/words is a standard file in linux system
The idea is to print random lines from the "words" file.
The random numbers are generated by using RANDOM variable.

The script:


#!/bin/sh

WORDFILE="/usr/share/dict/words"
NUMWORDS=5

#Number of lines in $WORDFILE
tL=`awk 'NF!=0 {++c} END {print c}' $WORDFILE`

for i in `seq $NUMWORDS`
do
rnum=$((RANDOM%$tL+1))
sed -n "$rnum p" $WORDFILE
done


Executing:


$ ./rword.sh
McLuhan's
Minnie
chants
biceps's
circumcision's


Generate random numbers in BASH:

To generate 5 random numbers between 1 and 100:


$ for i in `seq 5`; do echo $((RANDOM%100+1)); done
42
11
74
47
24


More about RANDOM number generation can be found here

3 comments:

Unknown said...

Hi, me again! This time I need to use the Random function, but I don't know how to. I will explain my problem:

I have a 3 columns file like:

A B 1
B C 0
D E 1
F G 1
M L 0

The 3rd column have only 1 and 0. I know how many times the number 1 appear (let's say 60% and so 0 40%) and now I would like to associate a new number 1 or 0 in the 3rd column but with the same percentage (60% for 1 and 40% for 0). I thought of something like:
if "random number"< 0.60 then i = 1, else i = 0, but i don t know how to "extract" the random number. I thank you in advance,
Valerio

Unknown said...

@Valerio, thanks for the question. Would need some more details on this, not able to understand the question fully, would you mind putting some more details, sort of sample expected o/p, that will help. Thanks.

natophonic said...

Nicely presented, but I found that was never getting past words beginning with the letter 'c', and realized that this is because RANDOM is a 16-bit value, but my /usr/share/dict/words has 235886 words.

So instead I used

RANDOM_CMD='od -vAn -N4 -tu4 /dev/urandom'
and
rnum=$((`${RANDOM_CMD}`%$tL+1))

to grab words over the whole span of the dictionary.

© Jadu Saikia www.UNIXCL.com