hi guys, i'm wondering how to calculate a probability and make the program respond to the result.

for example, in this little text RPG i want the drop rate of certain items to differ after the player has killed an enemy, or the accuracy rate to change based on the "accucary" level or whatnot.

i can't seem to find the answer anywhere, any ideas? thanks in advance.

Hi,
You have to use probability related formula to calculate probability. For that you need to formulate the mathematical model of your problem.
Also read about Monte Carlo method and Las Vegas algorithm. It may help you.
If you are not able to solve the problem. Please explain your problem completely. As your problem statement is not clear

This sounded fun so I wrote a little RPG :P

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmGame : Form
  {
    public frmGame()
    {
      InitializeComponent();
    }

    private void TestProbability()
    {
      //We are using 1-10 for this simulator
      const int maxValue = 10;
      Dictionary<int, int> dict = new Dictionary<int, int>();
      for (int i1 = 1; i1 <= maxValue; i1++)
        dict.Add(i1, 0);


      //Generate 600 numbers valued 1-10
      const int loopMax = 600;
      for (int i1 = 0; i1 < loopMax; i1++)
      {
        int rnd = Weapon.GetRandom(maxValue);
        dict[rnd] = dict[rnd] + 1;
      }

      StringBuilder sb = new StringBuilder();
      foreach (KeyValuePair<int,int> kvp in dict)
      {
        sb.AppendLine(string.Format("'{0:F0}': {1:F0}     %{2:F2}",
          kvp.Key,
          kvp.Value,
          ((decimal)kvp.Value / (decimal)loopMax) * 100
          ));
      }
      
      MessageBox.Show("Probability breakdown: " + Environment.NewLine +
        sb.ToString());

    }

    

    private void button1_Click(object sender, EventArgs e)
    {
      //First lets give you a spread of the probability
      //This is just informational
      TestProbability();


      Weapon spoon = new Weapon("Spoon", 6);
      Enemy enemy = new Enemy("Tree", 12);

      //REMEBER THE ALAMOOOOOOOOOOOOOOOO!
      while (!enemy.Dead)
      {
        spoon.Attack(enemy);
      }

      MessageBox.Show("You killed him!");
    }
  }

  public class Weapon
  {
    private string name;
    private int damage;

    public string Name
    {
      get { return name; }
      set { name = value; }
    }
    public int Damage
    {
      get { return damage; }
      set { damage = value; }
    }

    private Weapon()
    {
    }

    public Weapon(string name, int damage)
      : this()
    {
      this.name = name;
      this.damage = damage;
    }

    public void Attack(Enemy enemy)
    {
      int rndHit = GetRandom(10);
      if (rndHit > 2) //This gives us an 80% chance of hitting [1,2]=miss [3,4,5,6,7,8,9,10]=hit
      {
        //this gives us a range of 1 thru damage. This way we dont always use the max
        //damage amount. You probably want to use weighted brackets for this.
        int damageAmt = GetRandom(this.damage);
        enemy.Health -= damageAmt;

        MessageBox.Show(string.Format("Hit {0} with {1} for {2:F0}",
          enemy.Name,
          this.name,
          damageAmt));
      }
      else
      {
      }
    }

    public static int GetRandom(int Maxvalue)
    {
      byte[] randomNumber = new byte[1];
      System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
      Gen.GetBytes(randomNumber);
      int rand = Convert.ToInt32(randomNumber[0]);
      return rand % Maxvalue + 1;
    }
  }

  public class Enemy
  {
    private string name;
    private int health;

    public string Name
    {
      get { return name; }
      set { name = value; }
    }
    public int Health
    {
      get { return health; }
      set { health = value; }
    }
    public bool Dead { get { return health <= 0; } }
    private Enemy()
    {
    }
    public Enemy(string Name, int Health)
      : this()
    {
      this.name = Name;
      this.health = Health;
    }
  }
}

This code uses probability to assert that an attack has an 80% chance of succeeding and the weapons damage does 1 thru damage amount IF the enemy was hit. Likewise when something dies just follow similar logic to drop an item.

Comments
out of this world!
This article has been dead for over six months. Start a new discussion instead.