Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 "odds"-questions

Status
Not open for further replies.

flaviooooo

Programmer
Feb 24, 2003
496
FR
Hey all,

I have 2 questions regarding odds, which I hope someone can help me with:

1) How can I create a function which returns me the following: I have percentage and based on that percentage it gives me the value 1 or 0.

So for example I pass 75% to the function, so there is 75% chance the function will return me a 1 and 25% chance that it returns me a 0...

2) This one seems to be more difficult to me.
The base concept is that I'm trying to create some kind of racing simulation "game". I have 3 racers, who each have a percentage chance of winning the race.
Now I would to run a function, that will randomly rank them in the finish based on these percentages.

For example:
John has 50% chance of winning
Mike has 20% chance of winning
Eric has 30% chance of winning

Then the function would do it's thing and we would get a ranking:
1. Eric
2. John
3. Mike

Eric has beaten the odds, and won the race!

How do I start on something like this??
 
I'll address the first issue. Since you all ready predetermined the outcome, one way to get your desired results is this way:
Let's say you want your 75% of the time to be a 1. You'd pass this figure to a function that would then create a table, one column, with 75 one's and 25 zero's. You'd then do a random shuffle, and pick off the top record. Obviously 75% of the time it should be a 1. However, this is probability. The first 25,000 times it might be 0. The next 75,000 times it might be a 1.
Some reading to help in the setup:

How to find N records in random order in Access 2002

How do I Retrieve a random set of records in Microsoft Access?
 
You need a "seed" for the Rnd(). From microsoft - A seed is an initial value used to generate pseudorandom numbers. For example, the Randomize statement creates a seed number used by the Rnd function to create unique pseudorandom number sequences.
 
This is a rand variable from a Bernouli trial. To create a random outcome from any probability distribution, draw a random number from a continous probability density function and run it through the cumulative density function on the distribution so

1)
Code:
Public Function rndBernouli(Optional p As Single = 0.5) As Integer
  If Rnd <= p Then
    rndBernouli = 1
  End If
End Function

2) For the second problem I made an assumption that 1st place is determined by probabilities p1,p2,and p3 but second and 3rd place are equally likely. In other words if Mike wins then Eric and John have equal chance to come in second eventhough John has a better chance to have come in first.

I would really do this using OOP and a user defined custom class and collection, but if you are not familiar with this then there is some overhead.

So I defined a user defined type.
Code:
Type Racer
  racerName As String
  place As Integer
  pWin As Single
End Type

Then a function to determine the winner
Code:
Public Function getWinner(p1 As Single, p2 As Single, p3 As Single) As Integer
  Dim R As Single
   If p1 + p2 + p3 <> 1 Then
    MsgBox "probablilities must equal 1"
    Exit Function
  End If
  R = Rnd
  'Pick the winner
  Select Case R
    Case 0 To p1
      getWinner = 1
    Case p1 To (p1 + p2)
      getWinner = 2
    Case (p1 + p2) To (p1 + p2 + p3)
      getWinner = 3
  End Select
 End Function

Then the simulation
Code:
Public Sub RunRace()
  Dim RacerA As Racer
  Dim RacerB As Racer
  Dim RacerC As Racer
  Dim intWinner As Integer
  Dim intSecond As Integer
  
  RacerA.racerName = "John"
  RacerB.racerName = "Mike"
  RacerC.racerName = "Eric"
  RacerA.pWin = 0.5
  RacerB.pWin = 0.3
  RacerC.pWin = 0.2
  
  intWinner = getWinner(RacerA.pWin, RacerB.pWin, RacerC.pWin)
  Select Case intWinner
    Case 1
      RacerA.place = 1
    Case 2
      RacerB.place = 1
    Case 3
      RacerC.place = 1
  End Select
  
  intSecond = rndBernouli
  
 If RacerA.place = 0 Then
    RacerA.place = intSecond + 2
    intSecond = -1 * intSecond + 1
  End If
  If RacerB.place = 0 Then
    RacerB.place = intSecond + 2
    intSecond = -1 * intSecond + 1
  End If
  If RacerC.place = 0 Then
    RacerC.place = intSecond + 2
  End If

  Debug.Print RacerA.racerName & " " & RacerA.place
  Debug.Print RacerB.racerName & " " & RacerB.place
  Debug.Print RacerC.racerName & " " & RacerC.place
End Sub
 
but second and 3rd place are equally likely." If you shuffled the deck once to get the winner, why can't you eliminate him, then shuffle the deck again to get second?
 
Yeah, that is what It does.

The winner is drawn from a 50,20,30 probability distribution and second place is 50,50.

But that may or may not be realistic. In truth if John (50%) does not win you might expect him to have a higher probability of coming in second than in third. And this probability of coming in second might be higher given that Eric (30%) won and not Michael(20%). So You would need to define join conditional probability distributions if this assumption is incorrect.
 
OK, I see what you are saying.
1)Call getWinner
2)Then keep calling the getWinner function until you get a different racer than the winner.

so basically the first time the chances are
.5,.3,and .2

then if racerA wins you next distribution would be
B=.3/.5 and C = .2/.5

racerB wins
A = .5/.7
C= .2/.7

racerC wins
A = .5/.8
B = .3/.8

Probably a better assumption
 
Since there's no replacement, I agree that the probabilities of the remaining two must change. The odds for second for the two must add up to 100% so the original odds are gone. Then comes the question, how are those odds calculated? Maybe Mike's got John's number, as the saying goes, and in previous contests John's always choked against Mike. Then look at this year's Triple Crown in horse racing. Maybe there's steroids, or no steroids as in the case of the Belmont, involved. I wonder how "realistic" his simulation will be?
 
MajP - interestingly your post wasn't there when I posted but it addresses my question that, time wise, is posted after yours. I have enough problems without time dilation.
 
Here is the code that uses the original first place probabilities as predicative of second place finish.
Code:
Public Sub RunRace()
  Dim RacerA As Racer
  Dim RacerB As Racer
  Dim RacerC As Racer
  Dim intWinner As Integer
  Dim intSecond As Integer
  
  RacerA.racerName = "John"
  RacerB.racerName = "Mike"
  RacerC.racerName = "Eric"
  RacerA.pWin = 0.5
  RacerB.pWin = 0.3
  RacerC.pWin = 0.2
  
  intWinner = getWinner(RacerA.pWin, RacerB.pWin, RacerC.pWin)
  Select Case intWinner
    Case 1
      RacerA.place = 1
    Case 2
      RacerB.place = 1
    Case 3
      RacerC.place = 1
  End Select
  
  Do
   intSecond = getWinner(RacerA.pWin, RacerB.pWin, RacerC.pWin)
  Loop Until Not (intSecond = intWinner)
  
  Select Case intSecond
    Case 1
      RacerA.place = 2
    Case 2
      RacerB.place = 2
    Case 3
      RacerC.place = 2
  End Select
  
  Select Case (intWinner + intSecond)
    Case 3
      RacerC.place = 3
    Case 4
      RacerB.place = 3
    Case 5
      RacerA.place = 3
  End Select
  
  Debug.Print RacerA.racerName & " " & RacerA.place
  Debug.Print RacerB.racerName & " " & RacerB.place
  Debug.Print RacerC.racerName & " " & RacerC.place
End Sub
 
Thanks for all the responses guys, your help was very appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top