4

I am making a bot that can respond to my messages.

If i send Hi! to the bot, it will answer With Well, hello there!. I was just wondering, what do I do to give the bot multiple choices of answers? Is there a way to pick a random item from a responses array using JavaScript?

3
  • 1
    Could you spend some time first to show the relevant code you have and what you have tried to make it work? Commented Feb 13, 2017 at 19:22
  • Have you looked at Math.Random Commented Feb 13, 2017 at 19:24
  • 1
    Possible duplicate of Getting a random value from a JavaScript array Commented Nov 25, 2017 at 16:46

5 Answers 5

20

Use Math.random * the length of the array, rounded down, as an index into the array.

Like this:

var answers = [
  "Hey",
  "Howdy",
  "Hello There",
  "Wotcha",
  "Alright gov'nor"
]

var randomAnswer = answers[Math.floor(Math.random() * answers.length)];

console.log(randomAnswer);

Sign up to request clarification or add additional context in comments.

Comments

2

You can use the _.sample method in lodash:

var responses = ["Well, hello there!", "Hello", "Hola", "Yo!", "What’s up?", "Hey there."];
console.log(_.sample(responses));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Comments

0

I can think of two ways:

Method 1:

  • Use Math.random() function to get the random number between(0-1, 1 exclusive).
  • Multiply it by the array length to get the numbers between(0-arrayLength).
  • Use Math.floor() to get the index ranging from(0 to arrayLength-1).

const answers = [ "Hey", "Howdy", "Hello There", "Wotcha", "Alright gov'nor" ]; const randomlyPickedString=answers[Math.floor(Math.random() * answers.length)]; console.log(randomlyPickedString);

Method 2:

  • The random(a, b) method is used to generates a number between(a to b, b exclusive).
  • Taking the floor value to range the numbers from (1 to arrayLength).
  • Subtract 1 to get the index ranging from(0 to arrayLength-1).

const answers = [ "Hey", "Howdy", "Hello There", "Wotcha", "Alright gov'nor" ] ;
const randomlyPickedString=answers[Math.floor(random(1, 5))-1]; console.log(randomlyPickedString);

For ease of understanding the code, I have written created an extra variable(randomlyPickedString). You can use the code without it too.

Comments

-1

There's no JavaScript "command" that allows you to do this. But what you can do, is pick an integer at random from 0 to the length of the array, and get the array of responses at that index:

var response = responses[ parseInt( Math.random() * responses.length ) ];

A more concise way to do this is:

var response = responses[ Math.random() * responses.length |0 ];

where | 0 indicates the bitwise-or with 0, which in this case just turns a floating point number (Math.random() returns values from 0 to 1) into its lowest integer

4 Comments

Instead of parseInt why not Math.floor?
that works as well, but I'm taking the examples for what he might end up starting with and what he might see online. If someone is advanced enough to know about Math.floor, they're probably advanced enough to know about tricks like |0
I disagree, rounding is taught in grade school but bit operations are taught in college.
actually, they haven't taught me about bitwise even in the university...
-1

You would first need an array of possible responses. Something like this:

var responses = ["Well hello there!","Hello","Hola!"];

You can then use the Math.random function. This function returns a decimal < 1, so you will need to convert it to an integer.

var responses = ["Well hello there!","Hello","Hola!"];
var responseIndex = Math.floor((Math.random() * 10) + 1);

Also, use the modulus (%) operator to keep your random number within the confines of your array indexes:

var responses = ["Well hello there!","Hello","Hola!"];
var totalResponses = responses.length;
var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;

Finally, lookup your random response in the array:

var responses = ["Well hello there!","Hello","Hola!"];
var totalResponses = responses.length;
var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
var response = responses[responseIndex];
alert(response);

1 Comment

Math.floor(Math.random() * array.length) is enough to generate a random index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.