11

Possible Duplicate:
Getting a random value from a JavaScript array

OK, so I have three variables here, each being rock, paper or scissors. Using JavaScript, how can I generate one of those words randomly?

So far it's like so:

<!DOCTYPE html>
<html>
    <body>
        <button type="button" onclick="myFunction()"> Click me</button>

        <script>
            function myFunction()
            {
                var c="Rock";
                var d="Paper";
                var e="Scissors";
            }
        </script>
    </body>
</html>

Then I'll have a variable called K, which will be the random word out of rock paper or scissors. So it'll be like so:

alert("The computer chose: " + k);

So how can I make JavaScript select randomly between the three variables, c, d and e?

3
  • 4
    Put them in an array then select a random index from the array. (Please look up the emphasized words.) Commented Nov 5, 2012 at 17:40
  • 2
    Take a look @ stackoverflow.com/questions/4550505/… Commented Nov 5, 2012 at 17:41
  • Given that OP didn't have an array in his question, I don't think the duplicate is well chosen (this being said, there are probably other similar questions). Commented Nov 6, 2012 at 13:32

3 Answers 3

35

Use:

var things = ['Rock', 'Paper', 'Scissor'];
var thing = things[Math.floor(Math.random()*things.length)];
alert('The computer chose:' + thing);

Demonstration


Just to precisely answer your question, supposing you really want to keep your three global variables, you could do this:

var c = "Rock";
var d = "Paper";
var e = "Scissors";
var thing = window['cde'.charAt(Math.floor(Math.random()*3))];
document.write('The computer chose: ' + thing);

Demonstration

(But don't.)

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

Comments

4

You can use Math.random() to get a random number beteween 0 and 1.

If you want a whole random number between 0 and 2. (so: 0, 1 or 2). You can use:

Math.floor(Math.random()*3);

Note that Math.round (instead of floor) would be wrong here since the edge values will have a lower chance, and you might actually get 3 as well.

Comments

1

You should make an array:

var words = ['Rock', 'Paper', 'Scissors'];

and then generate a random number between 0 and the length of the array, with 0 decimals:

var number = Math.floor(Math.random() * words.length);

And then select the word where the key is the random number you just created:

var word = words[number];

In total:

var words = ['Rock', 'Paper', 'Scissors'];
var word = words[Math.floor(Math.random() * words.length)];

2 Comments

-1 is not necessary. If you add that, it would never return Scissors and instead return undefined.
If fact, if you leave the -1 in the code, you will get NaN results about 1/words.length proportion of the time. I have removed the -1 from the short summary at the end.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.