10.03.2019 - Round 1 results of our "A Love Letter For FreeBASIC" game dev competition have been published. Please be sure to check the results thread: http://games.freebasic.net/forum/index.php?topic=629.0. Don't forget that the competition is continuing with a round 2, lasting till 29th of April, 300 USD first prize. Stay tuned!

### Author Topic: Probability Machine  (Read 3951 times)

#### notthecheatr

• Global Moderator
• Forum Sage
• Posts: 351
• Who's the guy from 21 Jump Street?
##### Probability Machine
« 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:
Code: [Select]
`'''''' ntc.AI probability machine - coin-flipping example''''#Include "ntc/pm.bas"Using ntc.AI.pmRandomize TimerDim As pmObject probMachine = pmObject()'Store how many times heads or tails is chosenDim As uInteger ht (1 To 2)'How many times to flip?Dim As uInteger numFlips'Add two choices, each with a 50% chanceprobMachine.addChoice(0.5)probMachine.addChoice(0.5)'Find out how many times to flip the coinPrint "How many flips do you want?"Input numFlips'Flip that many timesFor i As uInteger = 1 To numFlips  'We add 1 to the index because choose() returns 0 or 1.  ht(probMachine.choose() + 1) += 1Next i'How many times was each chosen?Print "Heads:  " + Str(ht(1))Print "Tails:  " + Str(ht(2))'Experimental probabilityPrint Str(ht(1)/numFlips)Print Str(ht(2)/numFlips)Sleep`
The funniest thing happened yesterday.

#### Lachie Dazdarian

• Double dipper
• Forum Sage
• Posts: 1308
##### Re: Probability Machine
« 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.
"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
• Posts: 193
• Let's rocK~!
##### Re: Probability Machine
« 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..

#### notthecheatr

• Global Moderator
• Forum Sage
• Posts: 351
• Who's the guy from 21 Jump Street?
##### Re: Probability Machine
« 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.
The funniest thing happened yesterday.

#### Xerol

• Recruit
• Posts: 26
##### Re: Probability Machine
« 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.

#### notthecheatr

• Global Moderator
• Forum Sage
• Posts: 351
• Who's the guy from 21 Jump Street?
##### Re: Probability Machine
« 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!
The funniest thing happened yesterday.