Probability Machine

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Probability Machine

Postby notthecheatr » Mar 10, 2008 18:15

This is a simple but hopefully useful probability machine. Its main use would probably be in game AI systems, but it can be used for a lot of things.

This is an object that provides a simple way to do probability simulations and such. It comes with documentation and an example. I've spent all morning working on it, but I can't guarantee that there are no bugs left in it - if you find anything let me know.

http://notthecheatr.phatcode.net/downlo ... _AI_pm.zip

Coin-flipping Example:

Code: Select all

''
''
'' ntc.AI probability machine - coin-flipping example
''
''

#Include "ntc/pm.bas"
Using ntc.AI.pm

Randomize Timer

Dim As pmObject probMachine = pmObject()

'Store how many times heads or tails is chosen
Dim As uInteger ht (1 To 2)

'How many times to flip?
Dim As uInteger numFlips

'Add two choices, each with a 50% chance
probMachine.addChoice(0.5)
probMachine.addChoice(0.5)

'Find out how many times to flip the coin
Print "How many flips do you want?"
Input numFlips

'Flip that many times
For i As uInteger = 1 To numFlips
  'We add 1 to the index because choose() returns 0 or 1.
  ht(probMachine.choose() + 1) += 1
Next i

'How many times was each chosen?
Print "Heads:  " + Str(ht(1))
Print "Tails:  " + Str(ht(2))

'Experimental probability
Print Str(ht(1)/numFlips)
Print Str(ht(2)/numFlips)

Sleep
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Mar 10, 2008 18:23

How is the probablity machine different from just using....

ht(int(RND*2))+=1 'Using array base 0

-Vince
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Mar 10, 2008 18:26

It's different in that you can use more than two choices and you can assign varying probabilities to each choice. The coin-flipping example is to show the simplicity, although in reality you're right, you could use that. But if you wanted to do something more complex, such as select from three choices with the following probabilities:
25%
25%
50%

You need something more complicated. That's what this is for.

You can write code to do this yourself, but this is nice because it uses OO features so it will make your code cleaner and simpler.
maddogg6
Posts: 824
Joined: Dec 07, 2005 22:58
Contact:

Postby maddogg6 » Mar 10, 2008 18:30

Its main use would probably be in game AI systems

Hmm, I got a 44% chance for that.

Strangely enough it didn't properly predict (100%) that I would use this to determine the likelihood to use this with a game AI tho... I think its buggy :p


j/k - pretty cool. :)
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Mar 10, 2008 18:36

notthecheatr wrote:It's different in that you can use more than two choices and you can assign varying probabilities to each choice. The coin-flipping example is to show the simplicity, although in reality you're right, you could use that. But if you wanted to do something more complex, such as select from three choices with the following probabilities:
25%
25%
50%

You need something more complicated. That's what this is for.

You can write code to do this yourself, but this is nice because it uses OO features so it will make your code cleaner and simpler.


That IS cool! I missed that the first time.

Thanks
-Vince
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Mar 10, 2008 18:47

Jolly Good!
JohnB
Posts: 236
Joined: Jul 22, 2005 3:53
Location: Minnesota Arizona

Postby JohnB » Mar 25, 2008 4:15

Thanks JohnB. Glad someone remembered my post :)


notthecheatr

What made me remember your program is one of the episodes of the twilight zone. The odds on flipping a coin are not 50-50. There is a very small chance the coin could stand on edge. And now I am going back to the twilight zone! The door opens, the door closes.

JohnB
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Mar 25, 2008 14:43

Oh, OK. So 49.99999-49.99999.

Actually, if the imprint on one side is heavier than that on the other side, you might actually have a slightly higher probability of getting the lighter side up. Apparently that's a problem with dice, since 6 is lighter than 1 and so on.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 7 guests