3 Replies - 16758 Views - Last Post: 21 February 2008 - 10:55 PM Rate Topic: -----

#1 grunerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-February 08

Dice Simulation

Posted 21 February 2008 - 01:55 PM

I am having an issue with a dice simulation problem I am working on. I am to build a dice simulator that covers the possible roll outcomes for 2 die, for n number of times the user chooses.

I have an array set up to hold this information, namely:

1. The outcome possibility (2-12),
2. The number of times that outcome occurs and
3. What percentage of total does it occur

I have the basic functionality of the program completed. I am, however, having trouble with sorting. I am to create a sort button that will change the way the array is sorted, by either outcome possibility or percentage of occurrance. Although the specifications state that I will use an array, I'm wondering if I need 2 arrays. Could someone steer me in the right direction?

Code follows in two files, Dice.cs class and Default.aspx.cs:

Dice.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for Dice
/// </summary>
public class Dice
{
	private Random random = new Random();
	public int count;
	

	public Dice()
	{
	}

	public int Roll()
	{
		int die1 = random.Next(1, 7);
		int die2 = random.Next(1, 7);
		return die1 + die2;
	}

	public int[] TossResults(int number)
	{
		int[] result = {0,0,0,0,0,0,0,0,0,0,0,0,0};

		for (int i = 0; i < number; i++)
		
			result[Roll()]++;
			return result;
		
	}

	public int CountRolls (int number)
	{
		for (int i = 0; i < number; i++)
			count++;
			return count;
	}

  


}




Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
	int numRolls;
	int[] diceThrows;
	int numCount;
	double pct;
	Dice dice = new Dice();
	   
	
	protected void Page_Load(object sender, EventArgs e)
	{
		txtRolls.Focus();
		cmdPercent.Enabled = false;
		cmdPossible.Enabled = false;
	   
	}

	public void setDiceArray()
	{
		numRolls = int.Parse(txtRolls.Text);
		diceThrows = dice.TossResults(numRolls);
		numCount = dice.CountRolls(numRolls);
		
		txtDiceOutcome.Text = "Outcome\tFrequency\tPercentage\n";
		for (int i = 2; i <= 12; i++)
		{
			double diceThrow = diceThrows[i];
			pct = (diceThrow / (numRolls * .01));
		   
			txtDiceOutcome.Text += i + "\t" + diceThrows[i] + "\t\t" +
				pct.ToString("#0.00") + " %\n";
		}
		
	}

	protected void cmdCalc_Click(object sender, EventArgs e)
	{
		setDiceArray();
		cmdCalc.Enabled = false;
		txtRolls.Enabled = false;
		cmdPossible.Enabled = true;
		cmdPercent.Enabled = true;
	   
	}

	protected void txtClear_Click(object sender, EventArgs e)
	{
		txtDiceOutcome.Text = "";
		txtRolls.Text = "";
		txtRolls.Focus();
		cmdCalc.Enabled = true;
		txtRolls.Enabled = true;
		cmdPercent.Enabled = false;
		cmdPossible.Enabled = false;
	}

	protected void cmdPossible_Click(object sender, EventArgs e)
	{
		cmdPossible.Enabled = false;
		cmdPercent.Enabled = true;
	}
	protected void cmdPercent_Click(object sender, EventArgs e)
	{
		cmdPercent.Enabled = false;
		cmdPossible.Enabled = true;

	}
}



I have read and tried the array.sort, but since I only have one item stored in the array I'll never be able sort any other way. I feel like I need a second array, but am am a little stuck. Any assistance in the right direction is very much appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Dice Simulation

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Dice Simulation

Posted 21 February 2008 - 03:27 PM

Lets take a dip into your Dice class for a minute and perhaps this will help clear any confusions you have. First of all, CountRolls should probably do nothing but return the value of "count" and take no parameters. When you do your TossRolls call and pass it the number of rolls to do, you can increment count then for each iteration of your loop where you are calling Roll(). At the end you have the number of times your rolled (the same as numRolls... you could even set count = numRolls).

Now as for your array sorting issue, you don't need two arrays because percentage is a calculated value. You can reach the value of percentage without needing to store it anywhere. So you just need an array of counts and with that you can figure out the percentage (take a count and divide it by the sum of all accounts in the array). Example...


// Calculate our sum of all values ONCE
int sum = 0;

for (int i = 2; i <= 12; i++) {
	 sum += diceThrows[i];
}

// Now loop through each count and divide it by the sum of all counts.
for (int i = 2; i <= 12; i++)
{
	 double diceThrow = diceThrows[i];

	 // Use the current diceThrow count and divide by the sum of all counts gives you the percentage.
	 pct = (diceThrow / sum);
		  
	 txtDiceOutcome.Text += i + "\t" + diceThrows[i] + "\t\t" + pct.ToString("#0.00") + " %\n";
}




So as you can see we calculate percentage on the fly because we know how many times a particular number came up on the dice and the total sum of values.

Eg. We roll a 12 two times and a 4 one time. So for the roll of 12 we have 2 (the value at diceThrows[12]) / 3 (the number of total rolls 2 rolls of 12 and 1 roll of 4). So we can say that 12 has been rolled 2/3 of the time or 66.6667%.

Now lets say we want to sort based on percentages, we just have to sort the counts in descending order because the highest counts are also going to have the highest percentages. So focus on sorting just the one array of counts and then loop through it to show percentages of occurrence. The outcome possibility will depend on your die themselves with each die having an equal possibility for each side, but together they can form pairs. The counts will also show you how possible certain pairs are... like 12 is only reached when both dice are 6. So out of 10 rolls if you ended up with 2 times of 12, you can say that pair has a possibility of 20%.

Hopefully that gives you a push in the right direction. :)

This post has been edited by skyhawk133: 21 February 2008 - 03:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 grunerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-February 08

Re: Dice Simulation

Posted 21 February 2008 - 05:03 PM

Thanks, Martyr2 for the information. I appreciate it.

I understand how to calculate the counts and percentages and already have it calculating correctly in my application. My problem is that I am having a problem sorting. When the application first runs and a user puts in a number to roll, it is to sort by outcome, which it does. I am to have 2 buttons that will change the sorting, one by percentage, and one back to outcome. My problem is that at the point that I click the button and trigger the event handler, my array has gone out of scope. If I make a call to my setDiceArray() again within the protected void cmdPercent_Click(), it isn't the same array. I'm trying to figure out how I can do an Array.Reverse(diceThrows) using the event handler for my two buttons, cmdOutcome and cmdPercent while my array is still in scope. Is this possible?

 public void setDiceArray()
	{
		numRolls = int.Parse(txtRolls.Text);
		diceThrows = dice.TossResults(numRolls);
		numCount = dice.CountRolls(numRolls);
	  

	   
		txtDiceOutcome.Text = "Outcome\tFrequency\tPercentage\n";

		for (int i = 2; i <= 12; i++)
		{
			double diceThrow = diceThrows[i];
			pct = (diceThrow / (numRolls * .01));
			

			txtDiceOutcome.Text += i + "\t" + diceThrows[i] + "\t\t" +
				pct.ToString("#0.00") + " %\n";

			cmdOutcome.Enabled = false;
			cmdPercent.Enabled = true;
			
		}
			
		
	}

	protected void cmdCalc_Click(object sender, EventArgs e)
	{
		setDiceArray();
		cmdCalc.Enabled = false;
		txtRolls.Enabled = false;
		cmdOutcome.Enabled = true;
		cmdPercent.Enabled = true;
	   
	}

	protected void txtClear_Click(object sender, EventArgs e)
	{
		txtDiceOutcome.Text = "";
		txtRolls.Text = "";
		txtRolls.Focus();
		cmdCalc.Enabled = true;
		txtRolls.Enabled = true;
		cmdPercent.Enabled = false;
		cmdOutcome.Enabled = false;
	}


	
	protected void cmdOutcome_Click(object sender, EventArgs e)
	{
		txtDiceOutcome.Text = "";
		txtDiceOutcome.Text = "Outcome\tFrequency\tPercentage\n";

		for (int i = 2; i <= 12; i++)
		{
			double diceThrow = diceThrows[i];
			pct = (diceThrow / (numRolls * .01));


			txtDiceOutcome.Text += i + "\t" + diceThrows[i] + "\t\t" +
				pct.ToString("#0.00") + " %\n";

			cmdOutcome.Enabled = false;
			cmdPercent.Enabled = true;

		}
	}
	protected void cmdPercent_Click(object sender, EventArgs e)
	{
		txtDiceOutcome.Text = "";
		txtDiceOutcome.Text = "Outcome\tFrequency\tPercentage\n";
		setDiceArray();

	}


}


Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Dice Simulation

Posted 21 February 2008 - 10:55 PM

Well if it is just a scope issue then you can bump up your array to the next higher scope. Make your array a form level variable array and then you can access its values between functions. In one function throw your rolls, load it into the form level variable, then when you go to sort your array will continue to have the same values as before.

If you find yourself needing something and it is going out of scope, then it is a clear flag that you just need to go to a higher scope with it.

That is the best solution but if you are strictly against it, then define the array in something like main, pass the array to the functions as needed. A function then acting on that array will change its value back in main because arrays are passed by reference.

Hope that helps. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1