CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Sep 2010
    Posts
    16

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Probability Function

    You mean, like /100?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2010
    Posts
    16

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Probability Function

    No, but a FUNCTION() might not be that hard... Create your own!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Probability Function

    Hint; if you generate a random number between 0 and 2, what are the odds that the result will be 1?

  6. #6
    Join Date
    Sep 2010
    Posts
    16

    Re: Probability Function

    Okay. I'm not sure where to start though.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Probability Function

    Did you not understand what I said above?

  8. #8
    Join Date
    Sep 2010
    Posts
    16

    Re: Probability Function

    Quote Originally Posted by BigEd781 View Post
    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

  9. #9
    Join Date
    Sep 2010
    Posts
    16

    Re: Probability Function

    Then perhaps have a nested IF to say whether a Steal occurred or not.

  10. #10
    Join Date
    Jan 2010
    Posts
    1,133

    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 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.
    Last edited by TheGreatCthulhu; September 4th, 2010 at 02:19 PM.

  11. #11
    Join Date
    Sep 2010
    Posts
    16

    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.

  12. #12
    Join Date
    Jun 2008
    Posts
    2,477

    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&#37; 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 03:56 PM.

  13. #13
    Join Date
    Sep 2010
    Posts
    16

    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.

  14. #14
    Join Date
    Jan 2010
    Posts
    1,133

    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:
    Quote Originally Posted by jmcewan View Post
    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 100-sided die" - a FRP analogy once again - and here, the equivalent is the random number generator.

  15. #15
    Join Date
    Sep 2010
    Posts
    16

    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?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured