Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JavaScript choose one of three options at random? [duplicate]

I am learning JavaScript and I want to make a simple rock paper scissors game. I want to make the computer choose the number 1, 2, or 3 at random and make the answer a variable called computerResponse. How do I do this?

var computerResponse = ???;

If this question is unclear please tell me and I will try to make it better.

Thanks in advance.

like image 236
Pancake_Senpai Avatar asked Sep 25 '13 19:09

Pancake_Senpai


1 Answers

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

var computerResponse = getRandomInt(1, 3);

Taken from MDN's documentation on Math.random().

like image 117
theftprevention Avatar answered Oct 03 '22 15:10

theftprevention