0

I have figured out how to loop through a string (sorta), although I can't seem to randomize each individual output. In my code, I use the follow line to sort through each string.

const data = [m1, m2, m3, m4, m5];
const randomElement = data[Math.floor(Math.random() * data.length)];

Although, in my code that uses the const "randomElement", it randomizes the first output but stays the same all throughout.

setInterval(function() {
  minecraftBot.chat(randomElement);
  console.log("Sent Message " + randomElement)
}, interval);`

How can I randomize each individual output?

I was trying to randomize each output.

I.e :

m1
m5
m4
m3

Etc.

Although in my code it only randomizes it like this:

m1, m1, m1, m1, etc
or m4, m4, m4
  • 9
    You need to move the line `const randomElement = data[Math.floor(Math.random() * data.length)];` to inside the `setInterval` callback so it is calculated each time. – 001 Apr 06 '23 at 13:15
  • [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array/2450976?r=SearchResults&s=1%7C675.8469#2450976) – Thomas Apr 06 '23 at 13:38

0 Answers0