Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
How to make 4 variables always take different values from an array?

I want to make it so that each one of these four variables always takes a different "name" value taken from the array. I wanted to write it so that it check whether the variable is equal to any of the other ones, and if it is, it re-randomizes it to be different. So far I've got this:

const arr = [{name:"a"},{name:"b"},{name:"c"},{name:"d"},{name:"e"},{name:"f"}] 

function myFunction()
{
let var1 = arr[Math.floor(Math.random() * arr.length)].name;
let var2 = arr[Math.floor(Math.random() * arr.length)].name;
let var3 = arr[Math.floor(Math.random() * arr.length)].name;
let var4 = arr[Math.floor(Math.random() * arr.length)].name;

while (var1 == var2 || var1 == var3 || var1 == var4)
{
var1 = arr[Math.floor(Math.random() * arr.length)].name;
}  
while (var2 == var1 || var2 == var3 || var2 == var4)
{
var2 = arr[Math.floor(Math.random() * arr.length)].name;
}
while (var3 == var2 || var3 == var1 || var3 == var4)
{
var3 = arr[Math.floor(Math.random() * arr.length)].name;
}    
while (var4 == var2 || var4 == var3 || var4 == var1)
{
var4 = arr[Math.floor(Math.random() * arr.length)].name;
}  
}

But it not only doesn't work, I've also go a problem where the variables can still be equal, because while the first "while" randomizes them to be different from others, the next "while"'s can set another variable do be equal to it.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Try just using one while loop, and test all the possibilities. This is not a scalable solution, but if you only need 4 unique values then this will do the job. You don't even need to init the variables, because from start they all undefined, so in the loop they will get values.

function myFunction()
{
let var1;
let var2;
let var3;
let var4;

while (var1 == var2 || var1 == var3 || var1 == var4 || var2 == var3 || var3 == var4 || var2==var4)
{
  var1 = arr[Math.floor(Math.random() * arr.length)].name;
  var2 = arr[Math.floor(Math.random() * arr.length)].name;
  var3 = arr[Math.floor(Math.random() * arr.length)].name;
  var4 = arr[Math.floor(Math.random() * arr.length)].name;
}  

console.log(var1);
console.log(var2);
console.log(var3);
console.log(var4);
}
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

First, randomly shuffle the array, then take the first four values from the array. This guarantees that you select four distinct elements from the array. It also scales much more easily to larger arrays or selecting more (or fewer) than four elements, and potentially performs better in edge cases.

let arr = [{name:"a"},{name:"b"},{name:"c"},{name:"d"},{name:"e"},{name:"f"}];
arr.sort((a, b) => Math.random() < 0.5);
let var1 = arr[0].name;
let var2 = arr[1].name;
let var3 = arr[2].name;
let var4 = arr[3].name;

Note that this modifies the order of elements in arr, so perform the sort operation on a copy of that array if you need to preserve the order of arr.

Also, while the very simple and easy-to-remember shuffle technique shown here is perfectly adequate for this very small example array, if you are shuffling a very large array you might consider well-known shuffling algorithms that perform better.


Another approach is to keep track of what indexes from the array you have randomly selected, and repeatedly generate new random indexes to avoid duplicates. This avoids modifying the arr array, which may be important depending on your use case.

let arr = [{name:"a"},{name:"b"},{name:"c"},{name:"d"},{name:"e"},{name:"f"}];

function takeUniqueRandom(arr, indexesUsed) {
    let index = -1;
    while (index < 0 || indexesUsed.includes(index)) {
        index = Math.floor(Math.random() * arr.length);
    }
    indexesUsed.push(index);
    return arr[index];
}

let indexesUsed = [];
let var1 = takeUniqueRandom(arr, indexesUsed).name;
let var2 = takeUniqueRandom(arr, indexesUsed).name;
let var3 = takeUniqueRandom(arr, indexesUsed).name;
let var4 = takeUniqueRandom(arr, indexesUsed).name;
about 4 years ago · Juan Pablo Isaza Denunciar

0

One possible way is to remove the selected element from the pool of available elements.

function myFunction() {
  const pool = [...arr]; // <-- make a shallow copy
  const numberOfRandomsToGet = 4; // <-- number of random elements to get
  const randomNames = []; // <-- store here the random names

  for (let n = 0; n < numberOfRandomsToGet; ++n) {
    let randomIndex = getRandomInt(0, pool.length - 1); // <-- generate random Index

    const randomElem = pool[randomIndex]; // <-- get random element at randomIndex
    if (randomElem) {
      pool.splice(randomIndex, 1); // remove selected element from the "pool"

      randomNames.push(randomElem.name); // add name
    }
  }
  console.log(randomNames); // dump generated random names to console
}

Working Stackblitz

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda