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.

return random nick from channel nicklist

Old posts that have not been replied to for several years.
Locked
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

return random nick from channel nicklist

Post by Dedan »

I have no idea how to even start to
return a random nick from channel nicklist


Thanks for any help given
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

EDIT:

Code: Select all

* censored to train your brain :P *
(I initially had code spoiling the point of roland's post)
Last edited by user on Thu Aug 14, 2003 5:01 pm, edited 2 times in total.
r
rolandguy
Voice
Posts: 25
Joined: Wed Aug 13, 2003 1:50 pm

Post by rolandguy »

first thing you need is a nicklist...

chanlist <channel> [flags[&chanflags]] # see doco

then find a random number between 0 and the length of that list (llength).

set mynum [rand $list_length]

then pull the nick at mynum in the list

set random_nick [lindex $chan_ppl $mynum]

hope this helps ;)

roland
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

set random_nick [lindex $chan_ppl $mynum]
Huh? How exactly do you extract that nick and from where did you got that $chan_ppl variable?

Tip:

Code: Select all

set position [expr [rand [llength [chanlist $chan]]] + 1]
Once the game is over, the king and the pawn go back in the same box.
r
rolandguy
Voice
Posts: 25
Joined: Wed Aug 13, 2003 1:50 pm

Post by rolandguy »

chanlist <channel> [flags[&chanflags]]

*please* read the doco...it's one of the eggdrop tcl commands, think you'll find everything you need there ;)

roland
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

caesar wrote:Tip:

Code: Select all

set position [expr [rand [llength [chanlist $chan]]] + 1]
since list commands are zero-based, the llength is always "last element index+1" and since rand returns a number between 0 and the supplied integer-1 no expr is needed.

Code: Select all

lindex [chanlist $c] [rand [llength [chanlist $c]]]
Keep in mind that an empty chanlist might be returned and cause rand to error on you (depending on what triggered the code of course), so you better check the llength first (or use expr rand() to do the randomizing)
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Umm.. yes, the code you've sugested should do the trick. Thanks for the tip.
Once the game is over, the king and the pawn go back in the same box.
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

Thanks for the help.
i am going to try this:

Code: Select all

    set spin_list [chanlist $chan]
    set spin_list [lreplace $spin_list $nick $nick]
    set spin_list [lreplace $spin_list $botnick $botnick]
    set spin_length [llength $spin_list]
    if {[$spin_length == 0]} {
    putserv "PRIVMSG $chan :You need more players"
    return 0
    }
    set spin_num [rand $spin_length]
    set spin_nick [lindex $spin_list $spin_num]

I am still working on that script for kaamal
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

The set spin_num [rand $spin_length] should be set spin_num [expr [rand $spin_length] +1] because of the rand. Example: rand 10 will allways be a number from 0 to 9 so.. by adding 1 to the result of the rand actualy is a random number from 1 to 10 inccluding 1 and 10, as it should be.
Once the game is over, the king and the pawn go back in the same box.
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

Code: Select all


    set spin_num [rand $spin_length] 
    set spin_nick [lindex $spin_list $spin_num] 

i thought the first element of the list was considered "0"
so, if the value of spin_num was 0,
wouldn't it select the first element of the list?
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Test this:

Code: Select all

.tcl putlog "[rand 10] [rand 10] [rand 10] [rand 10] [rand 10] [rand 10] [rand 10] [rand 10] [rand 10] [rand 10] [rand 10] [rand 10] [rand 10] [rand 10]"
and you'll see what I'm talking about.
Once the game is over, the king and the pawn go back in the same box.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Dedan -

Your code looks good except for this bit:

set spin_list [lreplace $spin_list $nick $nick]
set spin_list [lreplace $spin_list $botnick $botnick]

To remove $nick and $botnick from the list, you have to use lsearch to find it and then use that index to remove it.

set nick_idx [lsearch -exact $spin_list $nick]
set spin_list [lreplace $spin_list $nick_idx $nick_idx]

etc


Caesar -

Lists go from 0 to lastidx-1, so Dedan's code is right.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Umm.. you haven't got my point. I've said that the rand thing makes it choose a random number from 0 to the number you've have choosen to get a random number from it except it. Also, I do agree with you on the list part, I haven't said that the list part is wrong or something..
Once the game is over, the king and the pawn go back in the same box.
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

Code: Select all


    set spin_list "nick1 nick2 nick3 nick4 nick5"
    set spin_length [llength $spin_list] 

$spin_length is 5

Code: Select all


    set spin_num [rand $spin_length}

$spin_num is a number between 0-4

Code: Select all


    set spin_nick [lindex $spin_list $spin_num] 

[lindex]
This command treats list as a Tcl list and returns the index'th element from
it (0 refers to the first element of the list).

if $spin_num is 0, it would return the first element of the list (nick1)

if $spin_num is 4, it would return the fifth element of the list (nick5)

I don't see the problem Caesar, what am i missing?
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Nothing, after I've posted I've thinked a bit on the thinkgs stdragon said and he and now you where right. the lindex makes it be correct.. My appologies for that.
Once the game is over, the king and the pawn go back in the same box.
Locked