Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!