Home > OS >  How to randomise string from an array without having the same output twice in a row
How to randomise string from an array without having the same output twice in a row

Time:12-01

I am trying to generate a random string output from an array that's getting triggered at random times. The issue that I am facing now is, sometimes the same output comes out twice in a row, or even more than two times. I wanted the output to be different every single time it gets triggered. I don't want the next output to be the same as the very previous output. Please take a look at the code below. Thanks!

function randAnnouncement() {
  var lastIndex, ann = ["one", "two", "three"];
  let randomAnnouncement = ann[Math.floor(Math.random() * ann.length)];
  //speakTTS(randomAnnouncement);
  console.log(randomAnnouncement);
}

function init() {
  var myFunction = function() {
    randAnnouncement();
    var rand = Math.round(Math.random() * (5000 - 1000))   500; // generate new time (between x sec and x   500"s)
    setTimeout(myFunction, rand);
  }
  myFunction();
}

init();

CodePudding user response:

To ensure that the output of the randAnnouncement function is always different, you can use a variable to store the last generated output, and then check if the new output is the same as the last one. If it is, you can generate a new random output until it is different from the last one.

Here is an example of how you can do this:

let lastRandomAnnouncement;

function randAnnouncement() {
  var ann = ["one", "two", "three"];
  let randomAnnouncement = ann[Math.floor(Math.random() * ann.length)];

  // Check if the new output is the same as the last one
  if (randomAnnouncement === lastRandomAnnouncement) {
    // If it is, generate a new random output until it is different from the last one
    while (randomAnnouncement === lastRandomAnnouncement) {
      randomAnnouncement = ann[Math.floor(Math.random() * ann.length)];
    }
  }

  // Save the new output as the last one
  lastRandomAnnouncement = randomAnnouncement;

  //speakTTS(randomAnnouncement);
  console.log(randomAnnouncement);
}

function init() {
  var myFunction = function() {
    randAnnouncement();
    var rand = Math.round(Math.random() * (5000 - 1000))   500; // generate new time (between x sec and x   500"s)
    setTimeout(myFunction, rand);
  }
  myFunction();
}

init();

This will ensure that the output of the randAnnouncement function is always different, and that the same output is never generated twice in a row.

I hope this helps! Let me know if you have any further questions.

CodePudding user response:

What you are asking for is a random string generator without replacement on the last selected value. The only way to do this is to store the last value somewhere so you can exclude it or repeat the random selection if the same value is selected.

As the web is stateless (i.e. doesn't remember the last value when the page reloads), I suggest that you create a transient or store the last value and read it again before each test.

Another option is to increase the size of the dataset from which your random selection is made. In your example of only 3 values, there is a 33% chance that the same value is selected again. Whereas if you had say 200 values then there would be only 0.5% change of a repeat value occurring.

I hope that this helps you get a solution that you can use.

  • Related