🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Random enemy attack damage

Started by
2 comments, last by Hircinus 4 years, 7 months ago

In a web game I'm creating with JS, I need to randomly generate attack damage. This calculation is based on the enemy's level:


var lvl = Math.floor(Math.random() * 10) + 1; // Determine level of enemy [1, 10]
function attack() {
	...
    var player_dmg = Math.floor(Math.random() * 25) + 1 + (lvl - 1); // Determine attack damage [1*lvl, (1*lvl)+25]
    health_pts -= player_dmg;
  	...
}

However, what I want to achieve is the range [10 * lvl, (10 * lvl) + 25].

What could I do to get the range I want? I feel like the answer is right in front of my face but I can't for the life of me figure this out.

Advertisement

Just for the sake of clarity, it looks like this:


var player_dmg = Math.floor(Math.random() * 25) + 1 + (lvl - 1);

Could be expressed more simply as:


var player_dmg = lvl + Math.floor(Math.random() * 25);

Also, I'm not sure if the range in your comment is right. It seems like it should be [lvl lvl+24] rather than +25. Maybe I'm missing something though.

As for your question, for this sort of thing it's useful to have one or more 'random integer in range' functions. These are sort of easy to mess up, so I'm not going to guarantee correctness here.

You already know how to get a random integer in [0 max) (pseudocode):


floor(random() * max)

If you want a random integer in [min max):


min + floor(random() * (max - min))

And for [min max], which seems to be what you want:


min + floor(random() * (max - min + 1))

Once you have a 'random integer in inclusive range' function available, you can just write e.g.:


damage = randomIntInclusive(10 * lvl, 10 * lvl + 25)

 

39 minutes ago, Zakwayda said:

Just for the sake of clarity, it looks like this:



var player_dmg = Math.floor(Math.random() * 25) + 1 + (lvl - 1);

Could be expressed more simply as:



var player_dmg = lvl + Math.floor(Math.random() * 25);

Also, I'm not sure if the range in your comment is right. It seems like it should be [lvl lvl+24] rather than +25. Maybe I'm missing something though.

As for your question, for this sort of thing it's useful to have one or more 'random integer in range' functions. These are sort of easy to mess up, so I'm not going to guarantee correctness here.

You already know how to get a random integer in [0 max) (pseudocode):



floor(random() * max)

If you want a random integer in [min max):



min + floor(random() * (max - min))

And for [min max], which seems to be what you want:



min + floor(random() * (max - min + 1))

Once you have a 'random integer in inclusive range' function available, you can just write e.g.:



damage = randomIntInclusive(10 * lvl, 10 * lvl + 25)

 

Thanks! I'll try it out!

This topic is closed to new replies.

Advertisement