Random number, without sequencial duplicates


/ Published in: JavaScript
Save to your folder(s)

function prints a serie of random numbers but without the next one being the same as the previous. to print, just call the function in the body


Copy this code and paste it in your HTML
  1. <script type='text/javascript'>
  2. randomNum = 0;
  3. function noDuplicate(maxNum)
  4. {
  5. current = randomNum;
  6. while (current == randomNum) {
  7. current = Math.round(Math.random()*maxNum);
  8. }
  9. randomNum = current;
  10. return current;
  11. }
  12. </script>

URL: http://dandyland.org

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.