Zenithian
Zenithian

Reputation: 82

Math.Random() To pick random array

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();?

Upvotes: 1

Views: 90

Answers (1)

Joanvo
Joanvo

Reputation: 5817

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)

Upvotes: 1

Related Questions