Jump to content

Random generator


rahagnok
 Share

Recommended Posts

Hey everybody, I'm really new to javascript and I'm trying to make a simple game. I am having trouble with writing a random generator that will randomly generate a different die face each time the user clicks. I plan on showing five different die and each one needs to be able to generate a random image. Any help is greatly appreciated, thank you.

Link to comment
Share on other sites

You can't "generate" images through JavaScript, but if you had six images (one for each face) then you could do something like this:

<script type="text/javascript">	function roll() {		diebox = document.getElementById("diebox");		for (i = 1; i <=5; i++) {			//The dice are called die_1.gif, die_2.gif, die_3.gif, etc			roll = Math.floor(Math.random()*5) + 1;			diebox.innerHTML += "<img src=\"die_" + roll + ".gif\" alt=\"" + roll + "\" /> ";		}	}</script><div id="diebox">	<!-- This division will contain the dice images --></div><input type="button" value="roll" onclick="roll()" />

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...