user1884412
user1884412

Reputation: 21

How do I calculate the probable success rate in a game: x + random(12) => y

I'm trying to program a line of Ruby code that calculates the probable success rate of a simple skill test in a text adventure game. The test is "if x + random(12) => y then". How do I calculate the probable rate of success of this statement being true in Ruby?

In the game the player has certain skills and will occasionally have to test those skills plus a random number to get greater or equal to a given difficulty number. I want to calculate the success rate percentage of being able to win that skill test.

As an example in the adventure game your trying to track some animal through the jungle. To do this you must test your skill at tracking. If for example you have a tracking skill of 3 and you add a random number between 1-12 to that, you need to score at least a 9 or greater to succeed. Basically: Skill + random(12) => Difficulty_Number. I want to show a Success Rate percentage before they play to see what their chance of succeeding will be.

So in Ruby, what would be the algorithm to figure out the chance of success with my current Skill score? Thanks!

Upvotes: 1

Views: 612

Answers (1)

Benoît
Benoît

Reputation: 15010

You could do this.

def success_rate(skill, success_level, random_range=12)
  delta = success_level - skill

  return [100 - (delta.to_f / random_range * 100), 100].min.round(2)
end

Upvotes: 1

Related Questions