0

I am wanting to generate a random number between 1 and 10. Yet I am getting a couple of errors.

public Form1()
{
    InitializeComponent();
}
int randomNumber = (0, 11);
int attempts = 0;

public int RandomNumber
{
    get
    {
        return randomNumber;
    }

    set
    {
        randomNumber = value;
    }
}

it is all on the 0, 11 under the comma is says --> struct System.Int32 represents a 32-bit signed integer <--. Under the 11 it says --> Identifier expected Syntax error, ',' expected <--. Now if I just have like int randomNumber = 0; then it will work fine, still have multiple guesses and the guess count adds up like it should, and have the too high too low labels. just the number will always be 0.

Also how can I make it to where I don't have to click the guess button, I can just hit enter on the keyboard?

private void button1_Click_1(object sender, EventArgs e)
{
    try
    {
        if (int.Parse(textBox1.Text) > RandomNumber) label1.Text = "Too high.";

        else if (int.Parse(textBox1.Text) < RandomNumber) label1.Text = "Too low.";
        else
        {
            label1.Text = "You won.";
            textBox1.Enabled = false;
            label2.Text = "Attempts: 0";
            textBox1.Text = String.Empty;
            MessageBox.Show("You won in " + attempts + " attempts, press generate to play again.", "Winner!");
            attempts = 0;
            label2.Text = "Attempts: " + attempts.ToString();
            return;
        }
        attempts++;
        label2.Text = "Attempts: " + attempts.ToString();
    }
    catch { MessageBox.Show("Please enter a number."); }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Joe
  • 21
  • 1
  • 5
  • 4
    C# doesn't have a special syntax for producing random numbers. If you want to generate a random number, have a look at the `Random` class. – Luaan Apr 26 '16 at 18:09
  • 4
    http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c – daremachine Apr 26 '16 at 18:11
  • you can find the answer here: http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c – Breeze Apr 26 '16 at 18:14
  • "Also how can I make it to where I don't have to click the guess button, I can just hit enter on the keyboard?" That's a separate question that has nothing to do with random numbers. Ask it as a new question and you'll get better answers. – D Stanley Apr 26 '16 at 18:21

3 Answers3

1

You can use something like the below code to generate random number between 1 to 10

        Random randomNumberGenrator = new Random();
        int num = randomNumberGenrator.Next(10) + 1;
Maddy
  • 774
  • 5
  • 14
  • 1
    instead of replicating an answer you should find another question that answers the question and flag this one as a duplicate – Breeze Apr 26 '16 at 18:16
  • Sure Thanks for the info – Maddy Apr 26 '16 at 18:25
  • This initializes the `Random` instance with a seed of 10. It then tries to generate from `0` to `int.MaxValue - 1` and it then adds `1` to it. So this method is producing a value from `1` to `int.MaxValue` inclusively. – Enigmativity Jun 08 '16 at 12:30
  • @Enigmativity : Can I know what is the max value that I have defined as per your understanding. In the code I have defined my random number range and generating random numbers between 1 to 10 – Maddy Jun 08 '16 at 14:24
  • @Maddy - Actually, I was a bit wrong about the possible range of numbers - because you are initializing `Random` with the seed of `10` then it will always produce the same random numbers so the first call to `randomNumberGenrator.Next()` will always produce the same value. When I run your code I **always** get `2041175502`. If you removed the seed then you'll get different values between the number `1` and `2147483647` inclusively from your code. – Enigmativity Jun 08 '16 at 21:57
  • @Enigmativity : The question says generate random number between 1 to 10. And randomNumberGenrator.Next() will not always give you the same value. Please cross check before making any comment – Maddy Jun 09 '16 at 02:15
  • I agree that the question says to generate a number between 1 and 10, but your answer does not. When you instantiate `Random` with a parameter (i.e. `new Random(10)`) you are fixing the seeding value - which means it **always** will produce the same number the very first time you call `randomNumberGenrator.Next()`. You need to run your code and see. – Enigmativity Jun 09 '16 at 05:07
  • @Maddy - I ran your code myself to check so I know what I'm saying. – Enigmativity Jun 09 '16 at 05:08
  • @Enigmativity : There was a typo it should be the below statement int num = randomNumberGenrator.Next(10) + 1; – Maddy Jun 09 '16 at 12:50
  • @Maddy - I'm glad you got it sorted. :-) – Enigmativity Jun 09 '16 at 12:53
1

To generate a Random number you have to usw the System.Random class. Your syntax can look something like this :

System.Random rng = new System.Random(<insert seed if you want>);
int randomNumber = rng.Next(1,11);

You have to do rng.Next(1,11) since the lower bound is include (1 is a possible result) and the upper bound is exclude (11 isnt getting added into the pool oft possible results).

To do implement your Enter shortcut you have to add a method to your Forms KeyPress event in which you call the button1_clicked method.

button1_Clicked_1(this, System.EventArgs.Empty);

At last you have to set your forms "KeyPreview" property to true.

Tim
  • 31
  • 4
0

Take a look at this.

Use the Random class in order to generate a random number.

private Random _rnd = new Random(); 
    private int RandomNumber
        {
            get
            {

                return _rnd.Next(0,11);
            }

            set
            {
                this = value;
            }
        }
Community
  • 1
  • 1
Ali Khakpouri
  • 801
  • 8
  • 24
  • 2
    Not a good idea to create the `Random` object every time when the property is called. – joe Apr 26 '16 at 18:21