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.

randomizing a list

Old posts that have not been replied to for several years.
Locked
N
Nada_Surf

Post by Nada_Surf »

one can sort a list using the lsort command, is there a way to randomize the list... or a method of using the lsort -command option to randomize it??

thanks
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Post by egghead »

List on input, returns randomized list:

Code: Select all


proc randlist { list } {

   # determine length of list
   set listlength [llength $list]

   # iterate on list, decrease length per iteration 
   for { set i $listlength } { $i > 1 } { incr i -1 } {
      # choose random index 
      set randindex [rand $i]
      # pick the random item
      set randitem [lindex $list $randindex]
      # cut the random item from the list
      set list [lreplace $list $randindex $randindex]
      # paste the random item at the end of the list
      set list [lappend list $randitem]
   }
   return $list
}

To test the code from the partyline:

Code: Select all


bind dcc - randlist makerandlist

proc makerandlist { handle idx arg } {
   set list "1 2 3 4 5 6 7 8 9 10"
   putlog [randlist $list]
}

<font size=-1>[ This Message was edited by: egghead on 2002-02-01 20:04 ]</font>
Locked