Necesito generar un número aleatorio sin repetir un número anterior. Este es mi código:
getRandomInt( max:number, min:number ) { return Math.floor(Math.random() * (max - min + 1)) + min; }Me da el número aleatorio generado pero sigue repitiendo el mismo número.
Ejemplo:
1,2,3,4,4y en la segunda convocatoria:
1,12,3,14,15Fácilmente podrías hacer esto:
Object.defineProperty(Array.prototype, 'shuffle', { value: function() { for (let i = this.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)) [this[i], this[j]] = [this[j], this[i]] } return this } }) var numArray = Array.from(Array(10).keys()) //generate an array with number from 0 to 10 numArray.shuffle() el método .shuffle() tomará aleatoriamente uno a la vez de la matriz y lo imprimirá.