0

I am creating a Random Number Guessing game, but I have run into the problem of the Generator generating the same number unless I close it and reopen it. I have "Your number is too small/large". That works. But I want the number to change once the button has been clicked to check the guess. Any idea's of what is wrong?

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

namespace Guess_the_number_1
{
    public partial class Form1 : Form
    {
        int randomNumber;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Random random = new Random();
            randomNumber = random.Next(0, 10);

        }

        private void buttonCheckGuess_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(textboxGuess.Text) == randomNumber)
            {
                MessageBox.Show("Your Guessed Correctly! The Number Is: " + textboxGuess.Text);
            }
            else if (Convert.ToInt32(textboxGuess.Text) < randomNumber)
            {
                MessageBox.Show("The Number Is Larger Than: " + textboxGuess.Text);

            }
            else if (Convert.ToInt32(textboxGuess.Text) > randomNumber)
            {
                MessageBox.Show("The Number Is Smaller Than: " + textboxGuess.Text);

            }

            else
            {
                MessageBox.Show("Your Guessed Incorrectly. The Random Number Is Not: " + textboxGuess.Text);
            }
        }
    }
}
Jonathan Nixon
  • 4,940
  • 5
  • 39
  • 53
kbkasey
  • 33
  • 1
  • 1
  • 3

2 Answers2

4

Generate your first random number at load, and then again when the user has guessed the correct number.

Put your Random object as a field (class member) and use it in a method:

private void MakeNewRandomNumber()
{
    randomNumber = random.Next(0, 10);
}

Call this method at form load (where you are currently generating a number) and again in the click handler once the guess has been verified:

private void buttonCheckGuess_Click(object sender, EventArgs e)
{
    if (Convert.ToInt32(textboxGuess.Text) == randomNumber)
    {
        MessageBox.Show("Your Guessed Correctly! The Number Is: " + textboxGuess.Text);
        MakeNewRandomNumber();
    }
jltrem
  • 12,124
  • 4
  • 40
  • 50
0

You need to creat a random number each time this button is clicked not just once.

    int randomNumber;

    Random random;

    public Form1()
    {
        InitializeComponent();
        random = new Random();
    }

    private void Form1_Load(object sender, EventArgs e)
    {


    }

    private void buttonCheckGuess_Click(object sender, EventArgs e)
    {

        randomNumber = random.Next(0, 10);

        if (Convert.ToInt32(textboxGuess.Text) == randomNumber)
        {
            MessageBox.Show("Your Guessed Correctly! The Number Is: " + textboxGuess.Text);
        }
        else if (Convert.ToInt32(textboxGuess.Text) < randomNumber)
        {
            MessageBox.Show("The Number Is Larger Than: " + textboxGuess.Text);

        }
        else if (Convert.ToInt32(textboxGuess.Text) > randomNumber)
        {
            MessageBox.Show("The Number Is Smaller Than: " + textboxGuess.Text);

        }

        else
        {
            MessageBox.Show("Your Guessed Incorrectly. The Random Number Is Not: " + textboxGuess.Text);
        }
    }
}
Bit
  • 1,068
  • 1
  • 11
  • 20