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?

EssemCSH
  • 103
  • 1
  • 2
  • 13

5 Answers5

18

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);
Kit
  • 724
  • 4
  • 11
1

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>
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
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.

Abdulmoiz Ahmer
  • 1,953
  • 17
  • 39
-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

towc
  • 1,983
  • 2
  • 22
  • 36
  • Instead of parseInt why not Math.floor? – PaulBGD Feb 13 '17 at 19:27
  • 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` – towc Feb 13 '17 at 19:29
  • 1
    I disagree, rounding is taught in grade school but bit operations are taught in college. – PaulBGD Feb 13 '17 at 19:30
  • actually, they haven't taught me about bitwise even in the university... – avalanche1 Nov 28 '19 at 06:09
-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);
Matt Spinks
  • 6,380
  • 3
  • 28
  • 47