Forum OpenACS Q&A: Response to Coding: db vs tcl: Fastest Random Loop

Collapse
Posted by MaineBob OConnor on

Ok for you lurkers, here is the code that I ended up using.

set rand_num [ns_rand 6]

if { $rand_num == 0 } {
  set the_text "$txt1 $txt2 $txt3 $txt4 $txt5 $txt6"
} elseif {
     $rand_num == 1 } {
  set the_text "$txt2 $txt3 $txt4 $txt5 $txt6 $txt1"
} elseif {
     $rand_num == 2 } {
  set the_text "$txt3 $txt4 $txt5 $txt6 $txt1 $txt2"
} elseif {
     $rand_num == 3 } {
  set the_text "$txt4 $txt5 $txt6 $txt1 $txt2 $txt3"
} elseif {
     $rand_num == 4 } {
  set the_text "$txt5 $txt6 $txt1 $txt2 $txt3 $txt4"
} else {
  set the_text "$txt6 $txt1 $txt2 $txt3 $txt4 $txt5"
}

This code just rotates the show order and is not truly random. But for now, it does the job, doesn't hit the database and seems fast.

-Bob