How do I pick a word at random from a list of words?
Joe Smith
jes at mbio.med.upenn.edu
Sat May 20 11:10:59 AEST 1989
At the risk of starting a 'lets all write this in our favorite
langauge' blitz, here's an awk (sorry, 'new' awk or GNU awk) function
that print's an argument at random. It's easily modified to read a
file of words, if you need that.
------------------------------>8 cut here 8<------------------------------
#!/bin/sh
gawk '
BEGIN {
srand() # set rand() seed from current time
print rword(ARGV, ARGC)
exit
}
function rword (a, n) {
return a[ int(rand() * n + 1) ] # 1 <= n <= ARGC
}' $*
--
Joe Smith
University of Pennsylvania jes at mbio.med.upenn.edu
Dept. of Biochemistry and Biophysics (215) 898-8348
Philadelphia, PA 19104-6059
More information about the Comp.unix.questions
mailing list