1

Im working on a simple Russian roulette JavaScript game. All it is doing is printing Live or die to the screen but I want it to be random, currently I have an Array set up for the Live and Die printout but I want to generate a random number between 0-5 so it isnt a human entered. Here is my current that I have running.

<script>
 var randomnumber = math.random();
 var a = ['Die', 'Die', "Die", 'Live', 'Die', 'Die'];

 document.write('You ' + a[randomnumber]);
</script>

Can I have math.random(); in a var? and can I plug it in to the document.write();?

Samurai
  • 3,724
  • 5
  • 27
  • 39
Zenithian
  • 62
  • 1
  • 1
  • 7
  • yes, you can store the result of math.random in a var, but since you're not specifying any parameters, you get a floating point value in the range `[0,1)` which is useless as an array index. So, RTFM: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random – Marc B May 29 '15 at 17:00

1 Answers1

1

Math.random() generates a number between 0 and 1. To get one between 0 and 5 do as follows:

var randomnumber = Math.floor(Math.random() * 6)
Joanvo
  • 5,677
  • 2
  • 25
  • 35