javascript - Math.Random to draw image -
i trying use math.random draw image.
though know haven't got right.
i know need loop go through math.random function, don't know how implement it. know spawnrock function isn't correct not know how fix it. help?
this code breaks current drawimages.
var num; function mathrock() { var x = math.floor((math.random() * 10) + 1); document.getelementbyid("num").innerhtml = x; } function spawnrock() { if (num = 2, 4, 8){ context.drawimage(rock, 1500, 450); } else { } var rock = new image(); rock.src = "rock.png";
here's quick primer on math.random
math.random() * x
generate random floating point number between 0 (but not including) x. math.random*10
generate, example, 0.22, 1.56, 7.92 9.9999999999.
if need random integers, can strip off non-integer portion of random number parseint( math.random()*10 )
generate 0,1,2,3,4,5,6,7,8,9.
if want random "coin-flip" (true or false) can this:
var randomtruefalse=(math.floor(math.random()*2)==0);
if need do randomly 3 out of 10 times can this:
var thirtypercentsuccess=( (math.random()*10-7)>=0 );
then use desired version of random number choose whether draw or else:
if (thirtypercentsuccess){ context.drawimage(rock, 1500, 450); }else{ // else }
Comments
Post a Comment