Math.Random is not so random

I'm doing a basic game with popup boxes.
My problem is that if you look at the goblinAttack function, it repeats until the goblin is dead but the damage is random.
For example, I hit goblin 6's damage each time until it dies instead of a random number between 1 and 25 and the goblin deals 4 damage each time it hits. It's boring. I wanted it to be random each shot, but repeating the function seems to not give a random number each time.
  //VARIABLES var dmgMultiply = Math.floor(Math.random() * 10 + 1); var dmg = 10; var armour = 0; var hp = 100; //ENEMY VARIABLES var dmgGoblin = Math.floor(Math.random() * 10 + 1); var goblinHP = 100; //ARRAYS var choiceArray = ["Type 'sword' equip your sword.", "Type 'run' if you're scared.", "Type 'stats' to see your stats."]; var answerArray = ["sword", "run", "stats"]; var outcomeArrayOne = ["You equip your sword.", "You run away. Game Over."]; var outcomeArrayTwo = ["Sword Equipped."] //GAME CHOICE function choice(a, b, c, x, y, z, aa, bb) { var answer = prompt(a + "\n" + b + "\n" + c); if(answer == x) { alert(aa); swordEquipped(outcomeArrayTwo[0]); } if(answer == y) { alert(bb); } if(answer == z) { displayStats(); } } //EQUIPPED SWORD function swordEquipped(a) { dmg = 25; dmgMultiply = Math.floor(Math.random() * 25 + 1); alert(a + "\n" + "DMG : " + dmg); goblinCombatStart("goblin"); } //GOBLIN COMBAT START function goblinCombatStart(g) { alert("A wild " + g + " appears!" + "\n" + "The " + g + " has 100 HP!"); goblinAttack("goblin") } function goblinAttack(g) { alert("The " + g + " swings his axe at you!" + "\n" + "The " + g + " does " + dmgGoblin + " damage to you!"); hp -= dmgGoblin; var attack = prompt("Type 'attack' to swing your sword at the " + g + "\n" + "HP : " + hp); if(attack == "attack") { alert("You swing your sword at the " + g + "\n" + "You did " + dmgMultiply + " to the " + g); goblinHP -= dmgMultiply; alert("The " + g + " is on " + goblinHP + "HP"); if(goblinHP < 0) { goblinDead(); } if(goblinHP > 0) { goblinAttack("goblin"); } } else { alert("You dropped your sword an shouted " + "'" + attack + "'" + " in the goblins face. It wasn't very effective and the goblin choppped your head off"); } } function goblinDead() { alert("You have slain the puny goblin with only a few scratches!"); } //GAME START choice(choiceArray[0], choiceArray[1], choiceArray[2], answerArray[0], answerArray[1], answerArray[2], outcomeArrayOne[0], outcomeArrayOne[1]); //STATISTICS function displayStats() { var statCheck = confirm("HP : " + hp + "\n" + "ARMOUR : " + armour + "\n" + "DMG : " + dmg + "\n" + "Press OK to continue"); if(statCheck == true) { choice(choiceArray[0], choiceArray[1], choiceArray[2], answerArray[0], answerArray[1], answerArray[2], outcomeArrayOne[0], outcomeArrayOne[1]); } } 

READ MORE ARTICLES

0 Response to "Math.Random is not so random"

Posting Komentar