FreeBASIC Games Directory Forum
 Welcome, Guest. Please login or register. May 22, 2013, 10:20:14 PM 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length 11.5.2013 - Added a webpage for the latest FBGD competition.13.3.2013 - Members registrations temporary disabled. For all membership requests, please email me: lachie13@yahoo.com 30.11.2012 - The ninth issue of BASIC Gaming is out! Read it here: http://games.freebasic.net/forum/index.php?topic=560.022.11.2012 - Be sure to check our currently running annual FBGD game making competition. This year's theme is SEASONS OF THE YEAR, 300 \$ first place prize, and the competition runs till 18th of February. Link: http://games.freebasic.net/forum/index.php?topic=559.0

Pages: [1]
 Author Topic: Probability Machine  (Read 1688 times)
notthecheatr
Global Moderator
Forum Sage

Gender:
Posts: 351

Who's the guy from 21 Jump Street?

 « on: March 10, 2008, 12:32:58 PM »

(originally posted at http://www.freebasic.net/forum/viewtopic.php?t=10882)

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.

Coin-flipping Example:
''
''
'' 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

'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 "Tails:  " + Str(ht(2))

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

Sleep
 Logged

The funniest thing happened yesterday.
Lachie Dazdarian
Double dipper
Forum Sage

Gender:
Posts: 1195

 « Reply #1 on: March 10, 2008, 05:08:01 PM »

This is quite nice. Brings backs memories of my college Statistics classes.

Thanks for the documentation.

Still, I have no idea where I might be using this...for now.
 Logged

"Things like Basic and Free Basic provide much-needed therapy and a return to sanity and a correct appreciation of people. The arrogant folk really hate a word like 'Basic' - fine, and good riddance." ~ pragmatist
nkk_kan
Forum Howler

Gender:
Posts: 180

Let's rocK~!

 « Reply #2 on: March 11, 2008, 06:51:35 AM »

Probably, somewhere where you need to find random numbers...
but you want something better than rnd..

 Logged

notthecheatr
Global Moderator
Forum Sage

Gender:
Posts: 351

Who's the guy from 21 Jump Street?

 « Reply #3 on: March 11, 2008, 03:02:20 PM »

Well this uses Rnd internally, but the nice thing about it is that it chooses from any number of options based on specific probabilities associated with each option.  So in a game you might have a monster that half the time wanders aimlessly but every 25% of the time he starts following you and the other 25% of the time he stands still and starts shooting.  Or something like that.  It can be used to control the states of anything you want, yet based on certain probabilities.  The nice thing is you can modify probabilities at any time, perhaps even during a state change initiated by the probability machine internally.
 Logged

The funniest thing happened yesterday.
Xerol
Recruit

Posts: 26

 « Reply #4 on: April 06, 2008, 10:46:31 PM »

I could see it being used in an RPG, where before you attack it displays the % chance to hit based on all the factors that go into it, while not actually calculating whether you hit or not.

In fact, I might just use this in an upcoming project. I'll let you know if I do.
 Logged
notthecheatr
Global Moderator
Forum Sage

Gender:
Posts: 351

Who's the guy from 21 Jump Street?

 « Reply #5 on: April 07, 2008, 11:24:51 AM »

Yes, it could easily be used for that sort of thing.

And yes, feel free to use it in any of your projects - and let me know if you do!
 Logged

The funniest thing happened yesterday.
Pages: [1]