This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

random number script

Old posts that have not been replied to for several years.
Locked
U
USG|Shinji

random number script

Post by USG|Shinji »

Can somone build me a script that give me a random number?
when I type !random 3 70 it display me a random number between 3 and 70

When you can build me one i be your slave :D
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Re: randomnumber script

Post by strikelight »

USG|Shinji wrote:Can somone build me a script that give me an randomnumber?
when I type !random 3 70 it display me a random number between 3 and 70

When you can build me one i be your slave :D

Code: Select all

bind pub - !random shins_rand
proc shins_rand {nick uhost hand chan text} {
  set range1 [lindex [split $text] 0]
  set range2 [lindex [split $text] 1]
  set range1check [string trim $range1 "0123456789"]
  set range2check [string trim $range2 "0123456789"]
  if {($range1 == "") || ($range2 == "") || ($range1check != "") || ($range2check != "")} {
    puthelp "PRIVMSG $chan :Usage: !random <min range> <max range>"
    return
  }
  set randnum [expr [rand [expr ($range2 + 1) - $range1]] + $range1]
  puthelp "PRIVMSG $chan :Your number is: $randnum"
}
This is untested code.. but you get the idea..
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

/me pulls out his WHIP

Code: Select all

# Define the script
proc display:randnum {nick uh hand chan arg} {
  # split the incoming var, for easier reading
  set coup [split $arg]
  #get a random number, using formular RAND = UPPER - (LOWER + 1)
  set rand [rand [expr [lindex $coup 1] - [expr [lindex $coup 0] + 1]]]
  #Now add LOWER + 1 on to the random number
  set rand [expr $rand + [expr [lindex $coup 0] + 1]]
  #print it to channel
  puthelp "PRIVMSG $chan :Random $rand"
}
#bind the command
bind pub - "!random" display:randnum
EG "!random 3 70"

Will prduce a random number between 3 and 70, excluding 3 and 70.
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

ppslim wrote:/me pulls out his WHIP

Code: Select all

# Define the script
proc display:randnum {nick uh hand chan arg} {
  # split the incoming var, for easier reading
  set coup [split $arg]
  #get a random number, using formular RAND = UPPER - (LOWER + 1)
  set rand [rand [expr [lindex $coup 1] - [expr [lindex $coup 0] + 1]]]
  #Now add LOWER + 1 on to the random number
  set rand [expr $rand + [expr [lindex $coup 0] + 1]]
  #print it to channel
  puthelp "PRIVMSG $chan :Random $rand"
}
#bind the command
bind pub - "!random" display:randnum
EG "!random 3 70"

Will prduce a random number between 3 and 70, excluding 3 and 70.
And EG "!random [die] now" will cause his bot to die. :o
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Rather true.
U
USG|Shinji

Re: randomnumber script

Post by USG|Shinji »

strikelight wrote:
USG|Shinji wrote:Can somone build me a script that give me an randomnumber?
when I type !random 3 70 it display me a random number between 3 and 70

When you can build me one i be your slave :D

Code: Select all

bind pub - !random shins_rand
proc shins_rand {nick uhost hand chan text} {
  set range1 [lindex [split $text] 0]
  set range2 [lindex [split $text] 1]
  set range1check [string trim $range1 "0123456789"]
  set range2check [string trim $range2 "0123456789"]
  if {($range1 == "") || ($range2 == "") || ($range1check != "") || ($range2check != "")} {
    puthelp "PRIVMSG $chan :Usage: !random <min range> <max range>"
    return
  }
  set randnum [expr [rand [expr ($range2 + 1) - $range1]] + $range1]
  puthelp "PRIVMSG $chan :Your number is: $randnum"
}
This is untested code.. but you get the idea..
what can i do that my bot say a error to the one who use the command?
( [17:59:20] <)Sarah> [16:00] Tcl error [shins_rand]: integer value too large to represent )

sorry for my bad english
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Re: randomnumber script

Post by strikelight »

USG|Shinji wrote:
strikelight wrote:
USG|Shinji wrote:Can somone build me a script that give me an randomnumber?
when I type !random 3 70 it display me a random number between 3 and 70

When you can build me one i be your slave :D

Code: Select all

bind pub - !random shins_rand
proc shins_rand {nick uhost hand chan text} {
  set range1 [lindex [split $text] 0]
  set range2 [lindex [split $text] 1]
  set range1check [string trim $range1 "0123456789"]
  set range2check [string trim $range2 "0123456789"]
  if {($range1 == "") || ($range2 == "") || ($range1check != "") || ($range2check != "")} {
    puthelp "PRIVMSG $chan :Usage: !random <min range> <max range>"
    return
  }
  set randnum [expr [rand [expr ($range2 + 1) - $range1]] + $range1]
  puthelp "PRIVMSG $chan :Your number is: $randnum"
}
This is untested code.. but you get the idea..
what can i do that my bot say a error to the one who use the command?
( [17:59:20] <)Sarah> [16:00] Tcl error [shins_rand]: integer value too large to represent )

sorry for my bad english

You can change:

Code: Select all

  set randnum [expr [rand [expr ($range2 + 1) - $range1]] + $range1]
  puthelp "PRIVMSG $chan :Your number is: $randnum"
To:

Code: Select all

  if {[catch {set randnum [expr [rand [expr ($range2 + 1) - $range1]] + $range1]}]} {
    puthelp "PRIVMSG $chan :Please use a lower and proper range."
  } else {
    puthelp "PRIVMSG $chan :Your number is: $randnum"
  }
Unfortunately, tcl has some limitations... Depending on your TCL version that your bot is using, it may or may not help to upgrade your TCL.
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Re: random number script

Post by egghead »

USG|Shinji wrote:Can somone build me a script that give me a random number?
when I type !random 3 70 it display me a random number between 3 and 70

When you can build me one i be your slave :D
USG|Shinji, there are a couple of questions/issues when writing such script.

1. The first question is whether the script should deal with integers only or floats?

2. The second question is if the script should be able to deal with negative integers?

3. The third question is whether the script must include the upper and lower integer as possible values?

4. The fourth question is how rigorous the script must check and respond to the userinput? What if the user types "!random 10 ahahah"?

5. If the script should be able to handle integers, one is bound by the maximum and minimum representable integer (MAXINT and MININT or whatever name they have on your machine :) ).
On most 32 bit machines this minimum and maximum are -2147483648 and 2147483647 respectively.

6. Additionally, suppose one allows negative integers on the aforementioned 32 bit machine.
An (idiotic) user types: "!random -1 2147483647" i.e. the script is to find a random integer between these two values. Commonly used intermediate calculations will subtract the upper integer from the lower integer: 2147483647 - - 1 = 2147483648. This resulting integer can not be represented correctly as an integer, because it exceeds the maximum integer value. In this particular case most likely it will be represented as -2147483648. The same happens when a commonly used method adds 1 to the upper integer and this integer happens to be 2147483647. This problem can be avoided by doing all the calculations using floats and using the [expr int()] or [expr round()] functions afterwards.

My experiment can be found at:
http://members.fortunecity.com/eggheadt ... er.tcl.txt

1. Script handles signed decimal integers.
2. Script handles negative integers.
3. Lower and upper integer are possible random numbers too.
4. Script will notify user on idiotic input.
5. Integers must be in the range of MININT and MAXINT. It is possible to extend this range substantially by switching from integers to a mix of strings and floats.
6. Script uses zero [catches] :P
Locked