none
Number Guessing Game

    Question

  • Hi, I am new to C#. I am originally a Visual Basic programmer and was wanting some help on a guessing game of my own. I was wanting to know how I would use the random function in my program to generate integer between 1 and 100. This program could either be a console app. or a Win app. However, I have no experience in Win app's in C#. Any help will be appreciated.
    Sunday, November 19, 2006 3:09 AM

Answers

  • pretty simple! use the Random class and give it a min and max value to generate a number with using the Next() method:

    Random theRandomGen = new Random();

    int numberChosen = theRandomGen.Next(min, max);

     

    in your case:

    Random theRandomGen = new Random();

    int numberChosen = theRandomGen.Next(1, 100);

     

    this will work for both WinForm's and Console application. To display it, convert the "numberChosen" to a string (numberChosen.ToString()) and show it to the user via Console.WriteLine (if Console app) or MessageBox (if winform)

     

    in Winform, create a new Windows application

    drag a button on the form.

    double click this button

    in this button event (button click event), copy and paste the code above and add this at the end of the last line:

    MessageBox.Show(numberChosen.ToString());

     

    run application

    press button

    tada! :-)

    Sunday, November 19, 2006 3:16 AM