JSFiddle - React, Tailwind, and code Playground
by c_kick
HTML
<input type="button" id="test" value="click me!">
JavaScript
// Generates a unique number from a range
// keeps track of generated numbers in a history array
// if all numbers in the range have been returned once, keep outputting random numbers within the range
var UniqueRandom = { NumHistory: new Array(), generate: function(maxNum) {
var current = Math.round(Math.random()*(maxNum-1));
if (maxNum > 1 && this.NumHistory.length > 0) {
if (this.NumHistory.length != maxNum) {
while($.inArray(current, this.NumHistory) != -1) { current = Math.round(Math.random()*(maxNum-1)); }
this.NumHistory.push(current);
return current;
} else {
//unique numbers done, continue outputting random numbers
alert('done outputting unique numbers, now just returning random number');
return current;
}
} else {
//first time only
this.NumHistory.push(current);
return current;
}
}
};
$(document).ready(function () {
$('#test').click(function() {
var $random = UniqueRandom.generate(10);
$(this).after($random);
});
});