Home > database >  Math.floor(Math.random() * replies.length); not working
Math.floor(Math.random() * replies.length); not working

Time:04-13

const replies = [
    "image 1",
    "image 2",
    "image 3",
    "image 4",
    "image 5"
];

const reply = Math.floor(Math.random() * replies.length);
const words = replies[reply]
   message.channel.send(words);

So i have this simple discord bot code which i supposed to reply with a random message from replies list, the bot works and everything but keep outputting the same message over and over instead of a random one (image 5)

CodePudding user response:

It looks to me like your code works!

const replies = [
    "image 1",
    "image 2",
    "image 3",
    "image 4",
    "image 5"
];

const reply = Math.floor(Math.random() * replies.length);
const words = replies[reply]
console.log(words)

If you just console out words variable this appears to work how you want it to. You can see in the snippet that each time you run the code, a random image is returned.

As @3limin4to0r mentioned, this may be an issue with reusing the same index if you are running this multiple times.

  • Related