954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to make a dice roll using windows form application or the console.

I am trying to make a program in which the user is prompted to input a number(max length of 4 digits), and then the program shall produce the number of outcomes of a two dice roll meaning that 1 roll includes two die. The outcomes could be 2-12 and I also have to show how many times the number occurs and the percent the numbers occurs. It should be a bell curve meaning that if a user types 1000 rolls, 7 should have the most out comes and then 6 and 8 and so on. I got part of the program going using my proctors help but the help made me more confused could any body help me?

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 DiceRoll_Final
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void rollDice_Click(object sender, EventArgs e)
{
AcceptButton = rollDice;
if (numRolls.Text.Trim() == "")
{
MessageBox.Show("Must enter number of dice rolls");
}
else if (numRolls.Text.Trim() == "0")
{
MessageBox.Show("Must enter a number greatre than 0");
}
int iRolls = int.Parse(numRolls.Text);
// this convert the number of rolls the person wants and assigns it to iRolls
Random randGen = new Random(iRolls);
int iDiceTotal = randGen.Next(1, 6) + randGen.Next(1, 6);
// this will add the two random values to be the equivalent of two dice
int[] numOccurrence = new int[11] {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
// this is an array that has the number of dice there are in
numOccurrence[iDiceTotal]++;
StringBuilder strMsg;
strMsg = new StringBuilder("# of times\n");
for (int i = 2; i <= 12; i++)
{
strMsg.Append(numOccurrence[i].ToString() + "\n");
}
}
}
}

vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 

Please use code tags. To do this, put [ c o d e ] [ / c o d e ] around your code. It makes it easier for us to read.

Seems like you need to put that randomizing code into a loop...

int[] numOccurrence = new int[12];

for (int i = 0; i < iRollcount; ++i)
    ++numOccurence[randGen.Next(1, 6) + randGen.Next(1, 6)];

double[] percentOccurence = new double[12];
for (int i = 0; i < 12; ++i)
    percentOccurence[i] = ((double)numOccurence[i] / (double)iRollCount) * 100d


It makes a little bit more sense to put your occurence count in a dictionary:

Dictionary<int, int> numOccurence = new Dictionary<int, int>();
Dictionary<int, double> percentOccurence = new Dictionary<int, double>();

for (int i = 0; i < iRollcount; ++i)
     ++numOccurence[randGen.Next(1, 6) + randGen.Next(1, 6)];
foreach (int iNum in numOccurence.Keys)
    percentOccurence[iNum] = ((double)numOccurence[iNum] / (double)iRollCount) * 100d


This way you arent wasting the 0 or 1 index for a value that will never happen. Basically a dictionary maps a key object to a value object, the object types defined in the declaration.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

Could be wrong here , but unless a dice has only 5 sides it should be randGen.Next(1, 7) instead of randGen.Next(1, 6)

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
Could be wrong here , but unless a dice has only 5 sides it should be randGen.Next(1, 7) instead of randGen.Next(1, 6)

That's right. The lower bound (the first number) isinclusive, while the upper bound is exclusive. Just another nuance to make programmers pull their hair out I guess.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

@skatamatic FYI: my hair is becoming very thin...:icon_smile:

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: