title: how to generate randomly UNIQUE alphanumric number which are 6 / 8 digit
body: <p>I'm sorry this might be duplicated post, but i really not understand how can i do this. In my system I need to generate a code(coupon) which are alphanumeric around 6 - 8 value, and if user key simply key in the code which is not under generated code(coupon) it will show error message.</p>
  
  <p>i saw alot of ppl using, but i failed to applied this during import the import java.util.UUID;</p>
  
  <pre><code>String uniqueID = UUID.randomUUID().toString(); //UUID method
  </code></pre>
  
  <p>What i have done so far only can generate a random number ( not expected result)</p>
  
  <h2>Javascript</h2>
  
  <pre><code>var keylist="abcdefghijklmnopqrstuvwxyz123456789";
  var temp="";
  function generatecoupon(plength){
  temp = "";
  
  for (i=0;i&lt;plength;i++)
      temp+=keylist.charAt(Math.floor(Math.random()*keylist.length));
  
  return temp;
  }
  
  function populateform(enterlength){
     document.mainfrm.COUPON.value=generatecoupon(enterlength);
  }
  </code></pre>
  
  <h2>html</h2>
  
  <pre><code>&lt;input type="button" value="Generate Coupon" onClick="populateform(this.form.thelength.value)"&gt;
  &lt;input type="hidden" name="thelength" size=3 value="6"&gt;     
  &lt;input name="COUPON" type="text" id="COUPON" size="20" maxlength="20"&gt;
  </code></pre>
  
  <p>Need a guide  in java code to generated random unique code and checked if the value key in if out of the generated value will show error message , Please any help would be appreciated</p>