Problem with random shader script

Hi, my goal is to assign one out of 6 shaders to my selection of objects in away that each of them is randomly selected one out of those 6. I wrote those script, but I am not getting the desired result: they are all assigned the same randomly picked shader!

What am I missing and how would I solve that?

Here is my script:

string $sel[] = ls -sl;
int $count = 'size($sel)';
    for ($i = 0; $i < $count; $i++)
        {
        $no = rand(6);
        if ($no <= 1)
            {
            sets -e -forceElement Sparkle1SG;
            }
        else if ($no <= 2)
            {
            sets -e -forceElement Sparkle2SG;
            }                
        else if ($no <= 3)
            {
            sets -e -forceElement Sparkle3SG;
            }                
        else if ($no <= 4)
            {
            sets -e -forceElement Sparkle4SG;
            }
        else if ($no <= 5)
            {
            sets -e -forceElement Sparkle5SG;
            }                
        else
            {
            sets -e -forceElement Sparkle6SG;
            }                

    }

Your help is much appreciated.

you're always assigning a shader to the same selection.

you could change your for loop to

string $tmp;
for ($tmp in $sel){
 // do something interesting with tmp
}

Uhm, sorry, i am not a very experienced scripter. I still don't really understand what I am doing wrong and how your loops is different. What is $tmp ?

thanks for the reply though.

Ok see

sets -e -forceElement Sparkle1SG;

operates on everything thats selected.  So what you do is you loop over many times everriding everything again and again. Only the last state counts.

So what you really want is as follows (written form my head not tested).

string $sel[] = `ls -sl`;
for ($item in $sel){
  sets -e -forceElement ("Sparkle"+((int)rand(6)+1)+"SG") $item;
}

The last item in all maya commands is list that specifies WHAT to operate on of you omit it then it just operates on the selection