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

234
Views
Math.floor(Math.random() * replies.length); not working
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)

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

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.

about 4 years ago · Santiago Gelvez 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!