• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Percentage of chance

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Meiji  Members

Posted 09 May 2014 - 11:17 AM

Hello,

So I'm working on a game using HeroCloud, and I have a question. There's a character with, say, 5% chance to hit his target critically. How do I accomplish that he actually has 5% chance to do something?

### #2fastcall22  Moderators

Posted 09 May 2014 - 11:31 AM

Not sure about HeroCloud, but generally you get a floating point number between 0 and 1, and test if it's equal to or lower than 0.05:
crit_chance = 0.05
if rand_float(0,1) < crit_chance
damage *= 1.5

For a more "natural" random, you increase the 5% chance by some rate for every failure and reset it when it succeeds:
if rand_float(0,1) < (crit_chance + error)
damage *= 1.5
error = 0
else
# Increase chance by 6.75%
error += crit_chance * 0.0675

EDIT: Juliean is correct, I done goofed.

Edited by fastcall22, 09 May 2014 - 12:12 PM.

zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

### #3Meiji  Members

Posted 09 May 2014 - 11:33 AM

Thanks.

### #4Juliean  Members

Posted 09 May 2014 - 11:55 AM

@fastcall22:

Does this code really do what you expect of it? You are adding crit-chance * 1.0675 to the error everytime, which will increase the chance of success rapidly (100% on the first failure, etc). Didn't you mean

   # Increase chance by 6.75%
error += crit_chance * 0.0675f;


or something along those lines?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.