repetir para numeros numero number generar entre entero codigo and aleatorios aleatorio javascript jquery random numbers repeat

para - Javascript: Número aleatorio de 5, no repetir hasta que todos hayan sido utilizados



random entre 0 y 10 javascript (4)

Estoy utilizando el siguiente código para asignar una clase aleatoria (de cinco) a cada imagen individual en mi página.

$(this).addClass(''color-'' + (Math.floor(Math.random() * 5) + 1));

Está funcionando muy bien, pero quiero hacerlo de modo que nunca haya dos de la misma clase seguidas.

Sería aún mejor si nunca hubiera dos iguales en una fila, y tampoco usara ninguna clase más de una vez hasta que se hayan usado los 5 ... Como en, elimine cada clase usada de la matriz hasta que todos ellos se han utilizado, luego comienza de nuevo, sin permitir que el último de los 5 anteriores y el primero de los 5 siguientes sean del mismo color.

Espero que tenga sentido, y gracias de antemano por cualquier ayuda.


Solo para explicar mi comentario a la excelente respuesta de jfriend00, puede tener una función que devuelve los miembros de un conjunto en orden aleatorio hasta que todos hayan sido devueltos, luego comienza de nuevo, por ejemplo:

function RandomList(list) { var original = list; this.getOriginal = function() { return original; } } RandomList.prototype.getRandom = function() { if (!(this.remainder && this.remainder.length)) { this.remainder = this.getOriginal().slice(); } return this.remainder.splice(Math.random() * this.remainder.length | 0,1); } var list = new RandomList([1,2,3]); list.getRandom(); // returns a random member of list without repeating until all // members have been returned.

Si la lista puede ser codificada, puede mantener el original en un cierre, por ejemplo

var randomItem = (function() { var original = [1,2,3]; var remainder; return function() { if (!(remainder && remainder.length)) { remainder = original.slice(); } return remainder.splice(Math.random() * remainder.length | 0, 1); }; }());


var used = []; var range = [0, 5]; var generateColors = (function() { var current; for ( var i = range[0]; i < range[5]; i++ ) { while ( used.indexOf(current = (Math.floor(Math.random() * 5) + 1)) != -1 ) ; used.push(current); $(" SELECTOR ").addClass(''color-'' + current); } });


Necesita crear una matriz de los valores posibles y cada vez que recupera un índice aleatorio de la matriz para usar uno de los valores, lo elimina de la matriz.

Aquí hay una función aleatoria de propósito general que no se repetirá hasta que se hayan utilizado todos los valores. Puede llamar a esto y luego simplemente agregar este índice al final de su nombre de clase.

var uniqueRandoms = []; var numRandoms = 5; function makeUniqueRandom() { // refill the array if needed if (!uniqueRandoms.length) { for (var i = 0; i < numRandoms; i++) { uniqueRandoms.push(i); } } var index = Math.floor(Math.random() * uniqueRandoms.length); var val = uniqueRandoms[index]; // now remove that value from the array uniqueRandoms.splice(index, 1); return val; }

Demostración de trabajo: http://jsfiddle.net/jfriend00/H9bLH/

Entonces, tu código sería este:

$(this).addClass(''color-'' + (makeUniqueRandom() + 1));

Aquí hay un formulario orientado a objetos que permitirá que más de uno se use en diferentes lugares en su aplicación:

// if only one argument is passed, it will assume that is the high // limit and the low limit will be set to zero // so you can use either r = new randomeGenerator(9); // or r = new randomGenerator(0, 9); 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; } }

Uso de la muestra:

var r = new randomGenerator(1, 9); var rand1 = r.get(); var rand2 = r.get();

Demostración de trabajo: http://jsfiddle.net/jfriend00/q36Lk4hk/


Puedes hacer algo como esto usando una matriz y el método de empalme :

var classes = ["color-1", "color-2", "color-3", "color-4", "color-5"]; for(i = 0;i < 5; i++){ var randomPosition = Math.floor(Math.random() * classes.length); var selected = classes.splice(randomPosition,1); console.log(selected); alert(selected); }