
September 3rd, 2010, 12:41 PM
#1
Probability Function
Hello all,
Is there a function that calculates the probability (in percentage) of an argument? I understand that this comment maybe a little vague, however I've only recently began programming so I'm having some issues expressing what I truly want.

September 3rd, 2010, 01:25 PM
#2

September 3rd, 2010, 01:32 PM
#3
Re: Probability Function
Yes. Perhaps I can explain myself better with an example:
Imagine a basketball game where players move/act based on stats. A defender may have his STEAL rating at 94, and the offensive player has a BALL HANDLING rating of 98. I would like to know if a function exist that calculates the probability of the defender taking the ball from the offensive player. Like if (based on the stats) the defender has a 31% chance of making the steal.

September 3rd, 2010, 02:14 PM
#4
Re: Probability Function
No, but a FUNCTION() might not be that hard... Create your own!

September 3rd, 2010, 02:35 PM
#5
Re: Probability Function
Hint; if you generate a random number between 0 and 2, what are the odds that the result will be 1?

September 3rd, 2010, 02:38 PM
#6
Re: Probability Function
Okay. I'm not sure where to start though.

September 3rd, 2010, 02:48 PM
#7
Re: Probability Function
Did you not understand what I said above?

September 3rd, 2010, 02:53 PM
#8
Re: Probability Function
Originally Posted by BigEd781
Did you not understand what I said above?
I was responding to dglienna's comment.
I do understand what you are saying, I'm just not sure I want to randomize the numbers. That would mean I would randomize (using the above example) the players Steal attribute anywhere between 92 to 96 and the the offensive player's ball handling 96  100.
I suppose if that is the only way then I definitely give it a try.
thanks you guys

September 3rd, 2010, 02:54 PM
#9
Re: Probability Function
Then perhaps have a nested IF to say whether a Steal occurred or not.

September 4th, 2010, 03:15 PM
#10
Re: Probability Function
jmcewan, you can't just have some numbers and a magic function that will calculate the probability of whether an event will happen, not without first defining some rules  what are all the possible events, what approach should be used in calculation? If I you wanted to find out the chance that it'll rain tomorrow, you wouldn't just throw a die, would you? You would have to consider how the actual weather conditions affect the outcome (and maybe develop a simplified model, because factoring all the stuff in would be impractical).
And once you have the probability, what will you do with it? It will not make anything happen on it's own, no if statement will make it work. You need something to actually happen with the same chance. So go back and reread BigEd781's post.
And try to answer it  even post what you think is the right answer here, why not? If you didn't get the hint, no shame there  at least you'll know where to go next from the feedback.
Also, in your basketball game scenario  you already have the probabilities in the form of stats (or these can be derived from the stats), so you don't need to really calculate them.
You just need make things happen, or not  and a random number generator is a good tool for that.
Last edited by TheGreatCthulhu; September 4th, 2010 at 03:19 PM.

September 5th, 2010, 12:04 PM
#11
Re: Probability Function
I really haven't been programming long so I'm not sure what I am suppose to be seeing. I must admit, I do feel a little foolish right now for not being able to come up with an answer.

September 5th, 2010, 04:29 PM
#12
Re: Probability Function
But you have the answer. If you generate a random number between 0 and 99, then the chance to steal becomes
Code:
if( randomNumber < 95 ) { }
Assuming that a steal rating of 96 gives a 96% chance to steal. Otherwise, just convert the stat of 96 to the percentage you want it to represent.
So, your code would look something like this:
Code:
Random r = new Random();
if( r.Next(100) < player.StealChance  1 )
{
// steal successful
}
Last edited by BigEd781; September 5th, 2010 at 04:56 PM.

September 5th, 2010, 04:54 PM
#13
Re: Probability Function
You are right....the answer was right there. I understand now. The problem I was having is thinking that a percentage value should be the answer whereas it should help to form the solution.
Thanks for being patient with me guys.

September 6th, 2010, 01:11 PM
#14
Re: Probability Function
BTW, in relation with what you said about the stats, if you were having trouble understanding how to correlate the STEAL & BALL HANDLING ratings, with the setup you described in your earlier post:
Originally Posted by jmcewan
Imagine a basketball game where players move/act based on stats. A defender may have his STEAL rating at 94, and the offensive player has a BALL HANDLING rating of 98. I would like to know if a function exist that calculates the probability of the defender taking the ball from the offensive player. Like if (based on the stats) the defender has a 31% chance of making the steal.
There's no universal approach, that is totally up to you. As a guide, you could use a method similar to those a lot of pen & paper FRP games use. In essence, they have the rulebook, which defines how to handle this  some numbers are added or subtracted or whatever, and a new number is derived from that, and this number is matched against a dice roll.
So, similarly, you would define your own rules.
For example:(1) CHANCE = STEAL  BALL_HANDLING/2, or(2)
// this is not actual code
// it is assumed that a stat value can range from 0 to 100
if STEAL == BALL_HANDLING == 0 then
CHANCE = 0
else if min(STEAL, BALL_HANDLING) == STEAL then
CHANCE = 100 * STEAL / BALL_HANDLING
else
CHANCE = 100 * (1  BALL_HANDLING/STEAL) or(3) Run a check for BALL_HANDLING first, diminished by a bad luck factor (that affects every stat; let's say it's 25). Then if the test fails, do the same for STEAL. If the test succeeds, the defense player has taken the ball. If not, the offensive player got really lucky. or(4) something else... Now, these all give different probabilities, and model the behavior of the system/simulation/game in a slightly different way.
Once you have decided what suits your needs best, and once you have the CHANCE calculated, you can "throw a 100sided die"  a FRP analogy once again  and here, the equivalent is the random number generator.

September 11th, 2010, 11:31 PM
#15
Re: Probability Function
Hello again,
Just and update. I've managed to create the function without any shortcomings. I did it as follows:
StealChance = RandomClass.Next() % 96
BallHandlingChance = RandomClass2.Next() % 98
if( StealChance >= BallHandlingChance)
{
Console.WriteLine.("Great steal by " +defenderName);
} else{
Console.Write.(+ defenderName " gets called for the reaching foul");
}
What do you guys think?
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
