Can jQuery provide the tag name?
You could try this:
if($(this).is('h1')){
doStuff();
}
See the docs for more on is().
$(this).attr("id", "rnd" + $(this).attr("tag") + "_" + i.toString());
should be
$(this).attr("id", "rnd" + this.nodeName.toLowerCase() + "_" + i.toString());