Page 1 of 1

Chance, Probability, Luck, Percentage calculation for games.

Posted: Tue Nov 23, 2010 8:16 pm
by Rescator
When making games (and sometimes other software) where you need a chance of something either happening or not, it can get somewhat complicated to make it work correctly.

You may want a pure lucky chance, like say the player either hitting or not hitting something.
A pure random pick of 0 or 1 (random number range pick of 0-1) will give around 50% chance.
But what about 90% chance of hitting, or 5% chance of hitting?
What if you game has modifier like a bonus, or skill increase or items that increase the player chance of hitting,
or maybe sometimes they may be hurt thus reducing their chance of hitting.

Trying to create a random number range to handle all this is impossible, and many end up making huge mistakes,
which can lead to frustrated players when they "know" they should have hit based on the character attributes and skill,
yet they seem not to hit.

For example a hit chance of 99% should hit 99 out of 100 shots, but how many games can you remember where this has been true?
And how many games have you noticed that 100% really doesn't mean 100%?

The Chance_Luck() routine takes a percentage of chance/probability value.
Set it to 0 or lower and you will always get False back, set it to 100 or higher and you will always get True back.
Set it to 1-99 and you will get true returned based on the probability,
so setting the value to 99 and then calling Chance_Luck() 100 times you should on average get true 99 times and false 1 time.
This is perfect for lucky chance checks where you might want to let the player either hit or miss, or get hit or missed but with a specific probability.

The Chance_Amount() routine takes a amount value and a percentage of chance/probability value.
Set the percentage to 0 or lower and you will always get amount of 0 back, set it to 100 or higher and you will always get the full amount back.
Set the percentage to 1-99 and you will get the amount back modified by the percentage probability,
so setting the percentage to 99 and amount to 1000 then calling Chance_Amount() will return 901 to 1000 as the amount.
This is perfect for damage dealt or damage received checks where you might want the player to always deal at least 90%-100% damage for example.

The routines are very simple, so what is the point of this being a Tips really?
The reason is that many confuses the idea of Chance and Probability, as chance always have a 50/50 chance of happening or not happening.
While probability is the likelihood of it happening or not. In other words probability is chance+bias.

So think about impossible as a synonym for improbable, and possible as a synonym for probable.
You may think it is impossible for humans to fly on their own, but its' not, it's simply highly improbable we'll be able to ever.
You may think eating will always be possible, but you could be wrong, we could evolve to not need to eat, eating is simply highly probably to remain a human necessity.

The illusionist Derren Brown once tossed ten coins all heads in a row, the probability of that is very low, how did he do it?
He and the TV crew spent over 9 hours, where he again and again started counting from 1, until 9 hours later he finally managed ten heads in a row.

So the next time you think that there is a probability of something happening 90% of the time, does not mean it actually will. There is a likelihood it will, but no guarantee.
But with the two routines below, you will be guaranteed to have the bias needed to make the chance match the probability.
Those playing your game will have no idea how, but they will feel that the game is fair.
As it is the presumed probability that people expect, rather than what is actually happening, chance.

Code: Select all

EnableExplicit

;© Roger "Rescator" Hågensen, EmSai 2010. http://www.EmSai.net/
;Creative Commons Attribution License,
;http://creativecommons.org/licenses/by/3.0/

;Chance of success or failure given the percentage of probability.
;If 0% chance or lower (negative modifer overload, penality), failure always.
;If 100% chance or higher (positive modifer overload, bonus), success always.
;Otherwise 1% to 99% chance (+/- modifiers etc.).
Procedure.i Chance_Luck(percentage.i)
	Protected result.i,chance.i,range.i
	If percentage>99
		result=#True
	ElseIf percentage>0 ;1% to 99% chance
 		range=100-percentage
 		chance=Random(99)+1 ;Kinda clunky but needed since the number range starts at 0 rather than 1.
		If	range<chance
			result=#True ;lucky chance
		EndIf
	EndIf
	ProcedureReturn result
EndProcedure

;Chance of full amount given the percentage of probability.
;If 0% chance or lower (negative modifer overload, penality), amount is 0 always.
;If 100% chance or higher (positive modifer overload, bonus), amount is full always.
;Otherwise amount is 1% to 99% (+/- modifiers etc.).
Procedure.i Chance_Amount(amount.i,percentage.i)
	Protected chance.f,result.i,range.i
	If percentage>99
		result=amount
	ElseIf percentage>0
 		range=100-percentage ;Get range based on percentage
		chance=Random(range-1)+1 ;Kinda clunky but needed since the number range starts at 0 rather than 1.
		chance=(chance+percentage)/100.0 ;Create the modifier based on percentage
		result=amount*chance ;Apply modifer
	EndIf
	ProcedureReturn result
EndProcedure

Define.i i,n,s,percentage

;Lucky chance test
n=100000
percentage=50
DisableDebugger
For i=1 To n
	s+Chance_Luck(percentage)
Next
EnableDebugger
Debug StrD(((s/n)*100.0))+"% lucky chance average with "+Str(percentage)+"% probability."

;Amount probability test
n=1000
percentage=90
Debug Str(Chance_Amount(n,percentage))+" of max amount "+Str(n)+" with "+Str(percentage)+"% probability."

Re: Chance, Probability, Luck, Percentage calculation for ga

Posted: Wed Nov 24, 2010 5:34 pm
by IdeasVacuum
Very well described and very useful code too - thanks for sharing Rescator.