Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
How to make a random number appear as many times as I want

So I want a random number (picture) from a code below to appear as much time as I want. In this case I am dealing with pictures. Let' say I want to make a picture named 1.png just to be printed on screen 4 times excatly and then let's say pictures 5.png and 6.png all together to be returned 10 times (it can be 3 times 5.png, 7 times 6.png and other ways we can get to 10) and so on for other examples. How can I do it, since I have no idea at all ?

I hope I explained the right way what I want, and I hope anyone can help, thank you for your help.

function RandomImage() {
  return (Math.ceil(Math.random() * 10)).toString() + ".png";
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you want to duplicate a string you could use a function like this

function repeat(item, number=1) {
  return Array.from({length: number}).fill(item)
}

You can then call it like:

repeat(RandomImage(), 3) // -> ["1.jpeg", "1.jpeg", "1.jpeg"]
about 4 years ago · Juan Pablo Isaza Relatório

0

You can do this by setting up a datastructure which keeps track of how many times a specific number has already been returned. For this purpose we can utilize JavaScript's Map object.

So the basic idea is this:

  • Inside the random number function we generate a random number
  • Look into the map how many times we already returned that number
  • In case it matches e.g. 1==10 times we go back to step 1
  • If there's no match increment the map entry for the given number and ultimately return the number

Here's an example:

let randomNumbers = new Map();
let maxNumbers = 10;
for (let a = 0; a <= maxNumbers; a++) {
  randomNumbers.set(a, 0);
}

function RandomImage() {
  let failed = false;
  let random;
  do {
    failed = false;
    random = Math.ceil(Math.random() * maxNumbers);
    switch (random) {
      case 1:
        if (randomNumbers.get(1) == 4) {
          failed = true;
        }
        break;
      case 5:
        if (randomNumbers.get(5) + randomNumbers.get(6) == 10) {
          failed = true;
        }
        break;
      case 6:
        if (randomNumbers.get(5) + randomNumbers.get(6) == 10) {
          failed = true;
        }
        break;

    }
  }
  while (failed);
  randomNumbers.set(random, randomNumbers.get(random) + 1);
  return random.toString() + ".png";
}

for (let a = 0; a < 50; a++) {
  console.log(RandomImage());
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda