2 Replies - 734 Views - Last Post: 19 August 2009 - 11:59 PM Rate Topic: -----

#1 mineeric123  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 12-November 08

Random number issue

Posted 19 August 2009 - 07:22 PM

Hello Everyone


I am having a little trouble with this program. The goal is to simulate a dice game. I am using a random number generator to generate
numbers for the dice. The issue is that if I run the program through the debugger it works fine, but if I run it by clicking the
button it generates the same random numbers for each label. I am not sure what I a missing? I am interested as to why this is keeps occurring.


Eric

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

		private void btnEnd_Click(object sender, EventArgs e)
		{
			
		}

		private void btnRoll_Click(object sender, EventArgs e)
		{
			int roll = Convert.ToInt32(lblRoll.Text);
			int point = Convert.ToInt32(lblPoint.Text);
			int rollCounter = 0;
			int nextRoll = 0;
			int die1 = rollDice();
			int die2 = rollDice();

			lblDie1.Text = die1.ToString ();
			lblDie2.Text = die2.ToString();
			if (roll == 0)
			{
				//roll the dice
				point = die1 + die2;
				// add 1 to the counter 
				rollCounter++;
				lblRoll.Text = rollCounter.ToString();

				//test to see if point is = 7 or 11
				if (point == 7 || point == 11)
				{
					lblWins.Text = "Winner";
				}
				//test to see if point is = 2 or 12 
				else if (point == 2 || point == 12)
				{
					lblWins.Text = "You lose!";
				}
				// set point and ask to roll dice again
				else
				{
					lblPoint.Text = point.ToString();
					lblWins.Text = "Please roll dice again";
				}
			}
			// what to do if roll != to 0 
			else
			{
				point = Convert.ToInt32(lblPoint.Text);
				nextRoll = die1 + die2;
				//test to see if point is = 7 or 11
				if (point == 7 || point == 11)
				{
					lblWins.Text = "You lose";
				}
				//test to see if point is = 2 or 12 
				else if (nextRoll == point)
				{
					lblWins.Text = "Winner";
				}
				// set point and ask to roll dice again
				else
				{
				   
					lblWins.Text = "Please roll dice again";
				}

			}

		}
		public int rollDice()
		{
			//
			Random myRandom1 = new Random();
			int Number = myRandom1.Next(1, 7);
			return Number;
		}

		
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Random number issue

#2 mineeric123  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 12-November 08

Re: Random number issue

Posted 19 August 2009 - 08:14 PM

View Postmineeric123, on 19 Aug, 2009 - 06:22 PM, said:

Hello Everyone


I am having a little trouble with this program. The goal is to simulate a dice game. I am using a random number generator to generate
numbers for the dice. The issue is that if I run the program through the debugger it works fine, but if I run it by clicking the
button it generates the same random numbers for each label. I am not sure what I a missing? I am interested as to why this is keeps occurring.


Eric

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

		private void btnEnd_Click(object sender, EventArgs e)
		{
			
		}

		private void btnRoll_Click(object sender, EventArgs e)
		{
			int roll = Convert.ToInt32(lblRoll.Text);
			int point = Convert.ToInt32(lblPoint.Text);
			int rollCounter = 0;
			int nextRoll = 0;
			int die1 = rollDice();
			int die2 = rollDice();

			lblDie1.Text = die1.ToString ();
			lblDie2.Text = die2.ToString();
			if (roll == 0)
			{
				//roll the dice
				point = die1 + die2;
				// add 1 to the counter 
				rollCounter++;
				lblRoll.Text = rollCounter.ToString();

				//test to see if point is = 7 or 11
				if (point == 7 || point == 11)
				{
					lblWins.Text = "Winner";
				}
				//test to see if point is = 2 or 12 
				else if (point == 2 || point == 12)
				{
					lblWins.Text = "You lose!";
				}
				// set point and ask to roll dice again
				else
				{
					lblPoint.Text = point.ToString();
					lblWins.Text = "Please roll dice again";
				}
			}
			// what to do if roll != to 0 
			else
			{
				point = Convert.ToInt32(lblPoint.Text);
				nextRoll = die1 + die2;
				//test to see if point is = 7 or 11
				if (point == 7 || point == 11)
				{
					lblWins.Text = "You lose";
				}
				//test to see if point is = 2 or 12 
				else if (nextRoll == point)
				{
					lblWins.Text = "Winner";
				}
				// set point and ask to roll dice again
				else
				{
				   
					lblWins.Text = "Please roll dice again";
				}

			}

		}
		public int rollDice()
		{
			//
			Random myRandom1 = new Random();
			int Number = myRandom1.Next(1, 7);
			return Number;
		}

		
	}
}



Was This Post Helpful? 0
  • +
  • -

#3 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Random number issue

Posted 19 August 2009 - 11:59 PM

About yor problem, the reason why this is happening is because you initialize the random class again for each roll of the dice. Since the random seed it's getting is based on the system time, and both dice rolls will very probably be within the same second, the same seed will be passed, resulting in the same sequence of numbers. Plus inititializing the class for every random number is a waste of resources. So the solution would be to initialize it once as a class variable, like so:

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 CrapsGame
{
	public partial class MainForm : Form
	{
		
Random myRandom1 = new Random();
//initialize random object here

public MainForm()
		{
			InitializeComponent();
		}

		private void btnEnd_Click(object sender, EventArgs e)
		{
			
		}

		private void btnRoll_Click(object sender, EventArgs e)
		{
			int roll = Convert.ToInt32(lblRoll.Text);
			int point = Convert.ToInt32(lblPoint.Text);
			int rollCounter = 0;
			int nextRoll = 0;
			int die1 = rollDice();
			int die2 = rollDice();

			lblDie1.Text = die1.ToString ();
			lblDie2.Text = die2.ToString();
			if (roll == 0)
			{
				//roll the dice
				point = die1 + die2;
				// add 1 to the counter 
				rollCounter++;
				lblRoll.Text = rollCounter.ToString();

				//test to see if point is = 7 or 11
				if (point == 7 || point == 11)
				{
					lblWins.Text = "Winner";
				}
				//test to see if point is = 2 or 12 
				else if (point == 2 || point == 12)
				{
					lblWins.Text = "You lose!";
				}
				// set point and ask to roll dice again
				else
				{
					lblPoint.Text = point.ToString();
					lblWins.Text = "Please roll dice again";
				}
			}
			// what to do if roll != to 0 
			else
			{
				point = Convert.ToInt32(lblPoint.Text);
				nextRoll = die1 + die2;
				//test to see if point is = 7 or 11
				if (point == 7 || point == 11)
				{
					lblWins.Text = "You lose";
				}
				//test to see if point is = 2 or 12 
				else if (nextRoll == point)
				{
					lblWins.Text = "Winner";
				}
				// set point and ask to roll dice again
				else
				{
				   
					lblWins.Text = "Please roll dice again";
				}

			}

		}
		public int rollDice()
		{
			//

			int Number = myRandom1.Next(1, 7);
			return Number;
		}

		
	}
}

This post has been edited by Renagado: 20 August 2009 - 12:13 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1