#--------------------------------------------------------------------- # randomnumber.tcl # TCL script for IRC bot eggdrop. # # Usage: !random # Script will generate a random number between the lower limit # integer and the upper limit integer. # # v0: 16-Oct-2002 # v1: 16-Oct-2002 #--------------------------------------------------------------------- package require eggdrop 1.6 package require Tcl 8.0 bind pub m|m !random pub:randomnumber proc pub:randomnumber { nick uhost hand chan text } { # scan out two substrings (lowstring and uppstring) # Note: although it is possible to use one integer scan only, # a two tier scanning process is used here. # First two string arguments are scanned and then each of # these is scanned for a positive integer. # The two tier process facilitates a better diagnosing of # user input. # For example "!random aaa 10" will fail when an integer scan # is applied directly since the first integer scan will fail on # the "aaa" and subsequently the second integer is not scanned. set scancount [scan $text "%s %s" lowstring uppstring] # review the scancount if { $scancount <= 0 } { puthelp "PRIVMSG $chan :Random number generator needs one\ or two integers. Example: !random 3 70." return 1 } # if one integer is given, assume the other 0. if { $scancount == 1 } { set uppstring 0 } # scan out the lower and upper integers. # scan for a 3 digit positive integer... # set rule {%3[0-9]} # ... or a signed decimal integer? set rule {%d} set errorlist [list] # it must be possible to scan out a signed decimal integer # and this scanned integer must be equal to the original string. # Newer versions of tcl have [string equal]. if {[scan $lowstring $rule lower] != 1 || [string compare $lower\ $lowstring] != 0 } { lappend errorlist "First argument is not a valid integer." } if {[scan $uppstring $rule upper] != 1 || [string compare $upper\ $uppstring] != 0 } { lappend errorlist "Second argument is not a valid integer." } # any errors while scanning for proper integers? if { [llength $errorlist] > 0 } { # indicate range between minint and maxint. set maxint [expr 0x7[string range [format %X -1] 1 end]] set minint $maxint incr minint lappend errorlist "Valid integers: between $minint and $maxint." # spit out the errors. puthelp "PRIVMSG $chan :[join $errorlist]" return 1 } # Two proper integers available! if { [string compare $lower $upper] == 0 } { puthelp "PRIVMSG $chan :A random number between $lower and\ $upper? Deeeppppp..." return 1 } # just in case, swap them. if { $lower > $upper } { set muppet $upper set upper $lower set lower $muppet } # generate a random number. Note that rand() generates a float in # the range [0,1) i.e. 0 inclusive, 1 exclusive. # round() will be used to include the limits. # Since postive integers larger than MAXINT can not be stored # as integers, (upper - lower) will fail if this subtraction is # larger than MAXINT. Convert to floats first. set random [expr rand()] set upprand [expr $upper * $random] set lowrand [expr $lower * $random] set randomnumber [expr round($upprand - $lowrand + $lower)] puthelp "PRIVMSG $chan :Random number between $lower and $upper:\ $randomnumber" return 1 } putlog "Random number version 0 loaded."