| View previous topic :: View next topic |
| Author |
Message |
bsdkid Voice
Joined: 02 Nov 2005 Posts: 16
|
Posted: Thu Dec 22, 2005 7:41 pm Post subject: help with random |
|
|
| Can you help me make [rand 20] without repeating till all have been used? |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
|
| Back to top |
|
 |
dwickie Halfop
Joined: 21 Aug 2004 Posts: 76 Location: /pub/beer
|
Posted: Thu Dec 22, 2005 8:11 pm Post subject: |
|
|
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 |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Thu Dec 22, 2005 11:16 pm Post subject: |
|
|
| 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 |
|
 |
metroid Owner
Joined: 16 Jun 2004 Posts: 771
|
Posted: Fri Dec 23, 2005 3:22 am Post subject: |
|
|
| 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 |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Fri Dec 23, 2005 4:36 pm Post subject: |
|
|
[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 |
|
 |
metroid Owner
Joined: 16 Jun 2004 Posts: 771
|
Posted: Fri Dec 23, 2005 5:32 pm Post subject: |
|
|
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 |
|
 |
bsdkid Voice
Joined: 02 Nov 2005 Posts: 16
|
Posted: Fri Dec 23, 2005 5:48 pm Post subject: |
|
|
thx guys! great job  |
|
| Back to top |
|
 |
|