Skip to content
Sign upLog in
← Back to Community

Chance for something to happen?

Profile icon
Hamburgr

Hello Coders,

In my code, I would like to add something such as crit-chance (Critical Chance) and miss-chance (Your chance of missing). Is there a way I can implement that in my code, and be able to change the percentage later in my code?

~ Hamburgr

Voters
Profile icon
Hamburgr
Comments
hotnewtop
Profile icon
Coder100

Sure!

let critChanceProbability = 5; // 5% chance let missChanceProbability = 10; // 10% chance if (Math.random() * 100 + 1 == critChanceProbability) // just got a crit! if (Math.random() * 100 + 1 == missChanceProbability) // just got a miss!
Profile icon
RYANTADIPARTHI

Make a random function to perform your task.

Profile icon
badst
x=10; Math.floor(Math.random()*x);

Assign x to anything you would like.
Remember that if you choose 10, it'll return a random number between 0-9, not 10. You can fix this by changing x to 11 or adding 1.