My suggestion, but havent tested for bugs...:
Code:
on *:TEXT:!quote*:#:{
  var %l = 1
  var %c = 0
  while ( $read([color:green]quote.txt[/color],w,* $+ $2 $+ *,%l) != $null ) {
    inc %l
    inc %c
  }
  msg $nick $read([color:green]quote.txt[/color],w,* $+ $2 $+ *,$rand(1,%c))
}