# TOMBOLA!! randomize text waves, excellent to choose the presenter or make a cookies list for lab meetings

I accidantally deleted the following code snipped posted July 5th, 2012 by purozongo. Fortunately, Adam Light found it in Google's cache, so here is it again:
//      ***TOMBOLA!!!***
// TOMBOLA!!! randomize the text wave "lista" in the wave "resultado"
// excelent to choose the presenter or cookies list for the lab seminars
// Germán Fernández, <a href="mailto:gfernandez@uchile.cl&quot; rel="nofollow">gfernandez@uchile.cl</a>, @2012

Function Tombola(lista)
Wave/T lista
variable i,j,k
string comp

do
j=round(abs((enoise( (numpnts(lista) -1) ) ) ) )
comp=lista[j]
if (k==1)
i+=1
endif
while (i<(numpnts(lista)))

end

Function check(onda,busca)
wave onda
string busca

FindValue/S=0 /TEXT=busca/TXOP=1  onda
if (V_value==(-1))
return 1
else
return 0
endif
end

Here is an alternate approach that can be modified to randomize the order of ANY type of wave. The idea is to use a randomized index permutation wave as a sort key. No string comparisons are necessary. As above, a new re-ordered wave is created, but the input could also be re-ordered in place.

function SortNames(inList)  // re-orders input text wave by randomized sort key
wave/T inList
variable Npts = numPnts(inList)

string outName = NameofWave(inList)+"_R"
Duplicate/O inList, \$outName    //  make new wave for re-ordered list
WAVE outList = \$outName

shuffle(Npts)       //  create random sort key
WAVE wPerm
IndexSort wPerm, outList    //  re-arrange the output list entries
end

function shuffle(Npts)  //  perform a random permutation of indices 0...Npts-1
variable Npts
make/O/N=(Npts) wPerm = p   //  will be used as sort key
variable i, j, temp
for(i=Npts-1; i>0; i-=1)
temp = wPerm[i]
j = round(  i/2+enoise(i/2)  ) // pick random index from remaining choices
wPerm[i] = wPerm[j] //  swap entries
wPerm[j] = temp
endfor
end

For information on random shuffling, see http://en.wikipedia.org/wiki/Knuth_shuffle.

And here is what I actually wanted to add as another approach:

make/n=(numpnts(lista))/o keyw=abs(enoise(1))

A
The method by awirsing is very concise and elegant. It should usually work.

I hate to be too picky, but in theory doesn't assigning any random values to elements of `keyw` allow for duplication of values? Extremely unlikely, but theoretically possible. How random is the sorting then if `keyw` may have some equal values?

My method constrains the random choices among remaining sort indices, not permitting index duplication.
Hi, thanks for repost my code.
s.r.chinn, i found very interesting your comment, if well this sniplet was writen just as a need of some method to sort our lab meetings could be a good method to "random sort" of text waves.

Germán.

Forum

Support

Gallery