JSFiddle - React, Tailwind, and code Playground
by jfriend00
HTML
<script src="http://files.the-friend-family.com/log.js"></script>
Random Numbers with No Repeats:<br><br>
JavaScript
function randomGenerator(low, high) {
if (arguments.length < 2) {
high = low;
low = 0;
}
this.low = low;
this.high = high;
this.reset();
}
randomGenerator.prototype = {
reset: function() {
this.remaining = [];
for (var i = this.low; i <= this.high; i++) {
this.remaining.push(i);
}
},
get: function() {
if (!this.remaining.length) {
this.reset();
}
var index = Math.floor(Math.random() * this.remaining.length);
var val = this.remaining[index];
this.remaining.splice(index, 1);
return val;
}
}
var r = new randomGenerator(0, 9);
for (var i = 0; i < 10; i++) {
log(r.get());
}