#!/usr/local/bin/tclsh # This program reads the file "/usr/dict/words", and then outputs # to stdout the specified number (size) of random words from the # dictionary file. By default, 1000 words are generated. # # Note: the programs takes several seconds to read the input # file, but then it is fairly quick at generating the output. global line if {$argc > 1} { puts stderr "usage: generate-words \[size\]" exit -1 } if {$argc == 1} { set size [lindex $argv 0] } else { set size 1000 } proc randomInit { seed } { global rand c set c 0 set rand(ia) 9301 ;# Multiplier set rand(ic) 49297 ;# Constant set rand(im) 233280 ;# Divisor set rand(seed) $seed ;# Last result } proc random {} { global rand c incr c set rand(seed) [expr ($rand(seed)*$rand(ia) + $rand(ic)) % $rand(im)] return [expr $rand(seed)/double($rand(im))] } proc randomRange { range } { expr int([random]*$range) } randomInit [pid] set ins [open /usr/dict/words r] set count 0 while {![eof $ins]} { gets $ins line($count) incr count } close $ins incr count -1 for {set i 0} {$i < $size} {incr i} { puts stdout $line([randomRange $count]) } # # eof generate-words #