Code: Select all
set file [open myfile.txt r]
set data [read $file]
close $file
set lines [split $data \n]
set randline [lindex $lines [rand [llength $lines]]]
Code: Select all
#---------------------------------------------------------------------
# randlist
# TCL script for IRC-bot eggdrop.
# TCL provides specific ordering (ascii, dictionary, integer etc...)
# of lists through lsort. The proc "randlist" randomizes a list.
# On input a list, returns a randomized list.
# v0: 02-Feb-2002
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# For testing purposes from the partyline.
#---------------------------------------------------------------------
bind dcc - randlist makerandlist
proc makerandlist { handle idx arg } {
set list "1 2 3 4 5 6 7 8 9 10"
putlog [randlist $list]
}
#---------------------------------------------------------------------
# Procedure randlist
#---------------------------------------------------------------------
proc randlist { list } {
# determine length of list
set listlength [llength $list]
# iterate on list, decrease length per iteration
for { set i $listlength } { $i > 1 } { incr i -1 } {
# choose random index
set randindex [rand $i]
# pick the random item
set randitem [lindex $list $randindex]
# cut the random item from the list
set list [lreplace $list $randindex $randindex]
# paste the random item at the end of the list
set list [lappend list $randitem]
}
return $list
}