Simplest fix is just to reverse these two instructions:
<xsl:attribute name="id">
<xsl:value-of select="java:java.lang.Math.abs(java:nextInt($rand om))"/>
</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
If you write two attributes with the same name to the same element, the second one wins.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference