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.

help with random

Help for those learning Tcl or writing their own scripts.
Post Reply
b
bsdkid
Voice
Posts: 16
Joined: Wed Nov 02, 2005 1:04 am

help with random

Post by bsdkid »

Can you help me make [rand 20] without repeating till all have been used?
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Repeat what?
d
dwickie
Halfop
Posts: 76
Joined: Sat Aug 21, 2004 8:53 am
Location: /pub/beer

Post by dwickie »

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

Code: Select all

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]"
}
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

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

Code: Select all

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: Select all

 tag when posting logs, code
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Post by metroid »

Code: Select all

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.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

[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.
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Post by metroid »

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

Code: Select all

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)
b
bsdkid
Voice
Posts: 16
Joined: Wed Nov 02, 2005 1:04 am

Post by bsdkid »

thx guys! great job :)
Post Reply