8

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?

Community
  • 1
  • 1
Logan545
  • 221
  • 1
  • 3
  • 7
  • 4
    Put them in an *array* then select a *random index* from the array. (Please look up the emphasized words.) –  Nov 05 '12 at 17:40
  • 2
    Take a look @ http://stackoverflow.com/questions/4550505/getting-random-value-from-an-array – Alex K. Nov 05 '12 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). – Denys Séguret Nov 06 '12 at 13:32

3 Answers3

33

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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
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.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
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)];
Cameron
  • 2,903
  • 1
  • 30
  • 31
Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • 3
    `-1` is not necessary. If you add that, it would never return `Scissors` and instead return `undefined`. – Matthew Jul 15 '14 at 05:19
  • 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. – Cameron Jul 06 '21 at 22:42