How to create instances at depth?

What i am trying to do is create instances at random depths, but without any of them getting created at a depth preoccupied by another mc, so i tried to do the following code(dont laugh at it), but the instances are all getting created at same depth…any idea what i should do to fix this?


while(not this.getInstanceAtDepth(player_depth)==undefined)
 {
    player_depth=Math.ceil(Math.random()*29);
 }       
pid=this.attachMovie("player_mc","pid_"+player_count,player_depth)