Thread: Probabilities
View Single Post
  #1  
Old 04-20-2007, 02:07 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Probabilities

PHP Code:
/****
  table: { {item, unit}, {item, unit}, ... }

(+) getRandom( table )
  - returns a given item from a list
(+) getProbability( table, item )
  - calculates the probability of a given outcome
****/

public function getRandomtable )
  {
  if ( 
table.type() != )
    return 
false;

  
temp.total 0;
  for ( 
temp.tableCelltable )
    
temp.total += temp.tableCell[1];

  
temp.random random(0temp.total);
  for ( 
temp.tableCelltable )
    {
    if ( 
temp.random temp.tableCell[1] )
      return 
temp.tableCell[0];

    
temp.random -= temp.tableCell[1];
    }
  return 
false;
  }

public function 
getProbabilitytablecell )
  {
  if ( 
table.type() != )
    return 
0;
  
  
temp.probability temp.total 0;
  for ( 
temp.tableCelltable )
    {
    
temp.total += temp.tableCell[1];
    if ( 
temp.tableCell[0] == cell )
      
temp.probability += temp.tableCell[1];
    }

  return ( 
temp.probability temp.total );
  } 
Basically uses a probability table with a randomizer.

This is useful when you have a list of objects that carry different weight... And you want to select a random object.

One classical example would be mining: The the higher classed minerals have a lower rate of obtaining it.

PHP Code:
mineTable = {
  {
"nothing"50},
  {
"iron"10},
  {
"gold"3},
  {
"diamond"1}
 } 
Nothing has a 50 in 64 chance of being seleted. iron, 10 in 64... etc etc.

I am planning on adding different probability functions to it later.
Reply With Quote