egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

help with random

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
bsdkid
Voice


Joined: 02 Nov 2005
Posts: 16

PostPosted: Thu Dec 22, 2005 7:41 pm    Post subject: help with random Reply with quote

Can you help me make [rand 20] without repeating till all have been used?
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Thu Dec 22, 2005 8:11 pm    Post subject: Reply with quote

Repeat what?
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
dwickie
Halfop


Joined: 21 Aug 2004
Posts: 76
Location: /pub/beer

PostPosted: Thu Dec 22, 2005 8:11 pm    Post subject: Reply with quote

one way how to do this (list putlog random numbers 0-20 without repeating)

Code:

set num "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20"
while {[llength $num]!=0} {
  set index [rand [llength $num]]
  putlog [lindex $num $index]
  set num "[lrange $num 0 [expr $index-1]] [lrange $num [expr $index+1] end]"
}
Back to top
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Thu Dec 22, 2005 11:16 pm    Post subject: Reply with quote

dwickie wrote:
one way how to do this (list putlog random numbers 0-20 without repeating)

Code:

set num "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20"
while {[llength $num]!=0} {
  set index [rand [llength $num]]
  putlog [lindex $num $index]
  set num "[lrange $num 0 [expr $index-1]] [lrange $num [expr $index+1] end]"
}

[string replace] should be used instead of manipulating lists
_________________
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code
Back to top
View user's profile Send private message Visit poster's website
metroid
Owner


Joined: 16 Jun 2004
Posts: 771

PostPosted: Fri Dec 23, 2005 3:22 am    Post subject: Reply with quote

Code:
set x ""
while {[llength $x] < 20} {
 lappend x [rand 20]
 set x [lsort -unique $x]
}


This code will also create a list of 20 different numbers. (20 not included)
If you want it to list the numbers from 19 to 0 or 0 to 19 just change the lsort slightly.
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Fri Dec 23, 2005 4:36 pm    Post subject: Reply with quote

[expr [rand 20] +1] to have from 1 to 20 (bouth included) not from 0 to 19.
_________________
Once the game is over, the king and the pawn go back in the same box.
Back to top
View user's profile Send private message
metroid
Owner


Joined: 16 Jun 2004
Posts: 771

PostPosted: Fri Dec 23, 2005 5:32 pm    Post subject: Reply with quote

Seeing as he said [rand 20] i didn't bother with 1 to 20 :p

Code:
set x ""
while {[llength $x] < 20} {
 set x [lsort -integer -unique [lappend x [expr [rand 20] + 1]]]
}


is an more compact version (not that it really matters)
Back to top
View user's profile Send private message
bsdkid
Voice


Joined: 02 Nov 2005
Posts: 16

PostPosted: Fri Dec 23, 2005 5:48 pm    Post subject: Reply with quote

thx guys! great job Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber