Random number guessing game

problems with random number

Page 1 of 1

3 Replies - 4439 Views - Last Post: 01 March 2009 - 07:38 PM Rate Topic: -----

#1 Joe Six-Pack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-November 08

Random number guessing game

Posted 01 March 2009 - 05:49 PM

Ok I have an assignment where I need to generate a random number and then have the user make guesses at it. The problem is my random number stays the same even after I close the window and rerun the program.

Another problem is I'm getting a message saying targetNum doesn't exist in the current context. I've seen code like this and mine seems the same, but gets that error. Any help is appreciated.

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

namespace GuessingGameCh8
{
	public partial class Form1 : Form
	{
			int numOfTries = 0;
			int guessNum;
		//int targetNum;
			

			
		

		public Form1()
		{
			InitializeComponent();
		}
		
		
		
	   
		
		private void Form1_Load(object sender, EventArgs e)
		{
			Random r = new Random();
			int targetNum = r.Next(0, 100);
			 
		}

		private void guessBtn_Click(object sender, EventArgs e)
		{
			 guessNum = int.Parse((txtGuess.Text));

				//Random r = new Random();
			 //int targetNum = r.Next(0, 100);
			//while(numOfTries < 20)
			//{
				if (guessNum < targetNum)
				{
					lblGuess.Text = "Your number is too low!";
					this.BackColor = Color.Blue;
					txtGuess.Clear();
					numOfTries++;
					//break;
				}
				else if (guessNum > targetNum)
				{
					lblGuess.Text = "Your number is too high!";
					this.BackColor = Color.Red;
					txtGuess.Clear();
					numOfTries++;
					//break;
				}
				else if (guessNum == targetNum)
				{
					lblGuess.Text = "You've Guessed Right!";
						
					numOfTries++;
					txtGuess.Clear();
					MessageBox.Show("Number of tries:" + numOfTries);
					//break;
				}
			//}
		}

	   

	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Random number guessing game

#2 SigurdSuhm  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 111
  • Joined: 05-August 08

Re: Random number guessing game

Posted 01 March 2009 - 07:26 PM

Your problem here seems to be concerning what is called "the scope" of objects. If an object is defined within a function, it will only exist within that function. What you will want to do is declare targetNum as a global variable within the class. Just like you have done at the top (you commented it out though). Then you can assign a value to it during the Form1_Load method.

targetNum = r.Next(0, 100);


instead of

int targetNum = r.Next(0,100);


Now the targetNum variable will exist as long as the form exists. Let me know if that didn't solve the problem.

Happy coding
Was This Post Helpful? 0
  • +
  • -

#3 Vunutus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 01-March 09

Re: Random number guessing game

Posted 01 March 2009 - 07:37 PM

What the above poster said about scope is correct, but I have a feeling the OP had the scope right in the first place and just overlooked that he pasted in comments as well. I glanced over the code, and I believe the problem is that no seed is given for the random object.

The C# random object can take on parameter, the seed. The seed in any random number generator is what lets it generate numbers in a non-predictable way (which really is predictable but in 99% of cases that doesn't matter). If you pass the Random object a constant seed (for example: "5"), it will always generate the same string of numbers. You could set it to "6" and get a completely different result set. This means that the seed must be a number that will never be the exact same as it was the last time the probem was run. The most common method of doing this is to retrieve some system value like the number of ticks since system startup.

I haven't tested this code, but it gives an example of how to seed the RNG. If that doesn't work googling for C# random seed should give a good solution.

Random r = new Random(DateTime.Now.Ticks);


This post has been edited by Vunutus: 01 March 2009 - 07:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 Joe Six-Pack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-November 08

Re: Random number guessing game

Posted 01 March 2009 - 07:38 PM

Thanks for that. But the random number still isn't changing. It gets stuck on a number. Right now it's 72 every time I run it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1