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.
Printable View
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.
You mean, like /100?
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.
No, but a FUNCTION() might not be that hard... Create your own!
Hint; if you generate a random number between 0 and 2, what are the odds that the result will be 1?
Okay. I'm not sure where to start though.
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
Then perhaps have a nested IF to say whether a Steal occurred or not.
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 re-read 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.
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.
But you have the answer. If you generate a random number between 0 and 99, then the chance to steal becomes
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.Code:if( randomNumber < 95 ) { }
So, your code would look something like this:
Code:Random r = new Random();
if( r.Next(100) < player.StealChance - 1 )
{
// steal successful
}
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.
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:
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)or
// 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)(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 100-sided die" - a FRP analogy once again - and here, the equivalent is the random number generator.
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?
I forgot to put in the (-25) roll penalty for the StealChance, however you have a general idea of what I've accomplished.
Isn't that just what you've tried to do with that code you posted?
Now you've got it all randomized.
The random class has a much more convenient method with the signaturepublic virtual int Next(and another one
int maxValue
);public virtual int Next(-- so you don't need to do what you've done with %.
int minValue, int maxValue
);
The convention is to write the names of the variables with a lowercase starting letter. I assume that RandomClass is an instance of the Random class? If so, it is an object, so the name you gave it is a bit misleading.
Anyway, you don't need to, or should use two random objects in this case - one will do just fine; the only thing you need to do is to call the Next(...) method twice.
The whole idea was to define an event such a "a steal has occurred", to define a way to calculate it's probability from the stats (in whatever way suits your needs), and to run a check against this probability, by generating a random integer number between 0 and 99 [including 99, which gives a total of 100 possibilities].
Or you could alternatively divide the probability you had expressed in percentage by 100, and then call rnd.NextDouble(), which would give you a greater precision.
P.S. And, when I say "to define" - I don't mean in code, but conceptually.
I assume the Steal and Handling probabilities have been calculated for players playing against exactly the same opposition, otherwise the %ages are pointless. I have 100% ball handling, having only picked a ball up once and got past one 12 year old. Does that mean I can get past a pro who has 68% steal?
That's up to you - what do you want to do with the 'chance of steal' ?
Perhaps credit the defender with +1 steal and change possession. Sounds like the perfect implementation of Finite State Machines.
I'm looking into another possible way to achieve what I want. I would like to compare the two teams' stats then have it generate realistic statistical output.
On the other forum i have posted you a new message. Hope it helps its more or less an complete way to simulate your game.