C# exercises and solutions-random number


Random number in C#

Exercise 1: Write a C# program to allow a user to guess a number( from 1 to 6) that will be randomly generated by computer.

The user is asked to input his/her number. Then the number will be compared with the random number. See the example below:

Enter your number: 2
You lost.
My number is: 4

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{


class Program
{


  static void Main(string[] args)
  {

      ranguess();

     Console.ReadLine();

  }

   public static void ranguess()
  {
        int yn, rn;
        Random rd = new Random(); //create random object
   
        Console.Write("Enter your guess number:");
        yn = int.Parse(Console.ReadLine());
        rn =rd.Next(1,7);//generate random number from 1 t 6
        Console.WriteLine(rn);
        if (yn == rn)
        {
          Console.WriteLine("You won.");

        }
      else
        {
           Console.WriteLine("You lost.");
           Console.WriteLine("My number is {0}.", rn);

        }


  }
 }

}


Exercise 2: Modify the C# program above in order to have chances to continue or stop guessing.

 

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{


class Program
{


  static void Main(string[] args)
  {

      ranguess();

     Console.ReadLine();

  }

   public static void ranguess()
  {
     int yn, rn;
     int con=1;
     Random rd = new Random(); //create random object
     while (con == 1)
      {
         Console.Write("Enter your guess number:");
         yn = int.Parse(Console.ReadLine());
         rn =rd.Next(1,7);//generate random number from 1 t 6
         Console.WriteLine(rn);
        if (yn == rn)
        {
          Console.WriteLine("You won.");

        }
      else
        {
           Console.WriteLine("You lost.");
           Console.WriteLine("My number is {0}.", rn);

        }

       Console.Write("Press 1 to continue:");
       con = int.Parse(Console.ReadLine());


    }

  }
 }

}





Comments

Shkar  comment

 Shkar

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

private void Form1_Load(object sender, EventArgs e)
{
Random r = new Random();
int x = 1;
x=r.Next(7);
MessageBox.Show(x.ToString());
if (x < 3)
{


MessageBox.Show("you lost");

}
else {


MessageBox.Show("you winner");



}






}

}
}







2018-01-20
shrill comment

 shrill

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace randomNumber
{
public class Program
{
public void Main(string[] args)
{
Random rnd = new Random();
int random = rnd.Next(1, 7);
int input;
string choice = "y";
while (choice == "y")
{
Console.WriteLine("Enter a number between 0 and 6");
input = int.Parse(Console.ReadLine());
if (input == random)
{
Console.WriteLine("You won");
break;
}
else
{
Console.WriteLine("You lost");

Console.WriteLine("Do you want to continue?");
choice = Console.ReadLine().ToString();
if (choice == "n")
{ break; }
else
continue;


}
}

}
}
}


2017-01-29
jamisco comment

 jamisco

Hey guys, instead of creating a variable for your while loop. You can set the expression to true in the while loop so instead if doing this
int life = 1

while (life == 1) {
stuff..
}

you can do this
while (true) {
stuff...
}

here is a link explaining this concept

https://social.msdn.microsoft.com/Forums/en-US/fe98c04f-2e63-49e8-a26a-ad2cb88865df/while-true-need-help?forum=csharplanguage


2016-12-30
said comment

 said

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandomNumber
{
class Program
{
static void Main(string[] args)
{
int status=1;
int randomNumber;
int UserNUmber;

while (status == 1)
{
Random newNo = new Random();

Console.WriteLine("Enter the guessing number :");
UserNUmber = Convert.ToInt16(Console.ReadLine());

randomNumber = newNo.Next(1, 7);

if (randomNumber == UserNUmber)
{
Console.WriteLine("You guessed rigth!");
break;
}

else
{
Console.WriteLine("You Guessed Wrong!!!");
Console.WriteLine("The number is {0}", randomNumber);
Console.WriteLine("\n....play again.....\n");
}
}

Console.ReadLine();
}
}
}


2016-01-22
Monir Hossain comment

 Monir Hossain

int PcNumber, UserNumber;
string press = "y";

Random RandomNumber = new Random(); /*Auto generated random
number procedures*/



start1: //Label
PcNumber = RandomNumber.Next(0, 9); //Storing PC number

Console.WriteLine("The Computer is thinking of a number between 0 and 9. Can you guess the number?\n");
start2:
UserNumber = Convert.ToInt32(Console.ReadLine()); //Stroing User Number

if (UserNumber >= 10)
{
Console.WriteLine("\nInvalid Number, Please Chosse (0-9)\n");
goto start2;
}
else if (UserNumber == PcNumber)
{
Console.WriteLine("\nYou won.\n");
Console.WriteLine("\nWanna start a new game ?\n");
Console.WriteLine("If yes then type 'y'.\n");
C:
press = Console.ReadLine();
if (press.ToLower() == "y")
{
Console.WriteLine("\n------------New Game----------------\n");
goto start1;
}
else
{
Console.WriteLine("\nInvalid character ! press 'y' to start a new game\n");
goto C;
}

}
else if (UserNumber < PcNumber)
{
Console.WriteLine("\nApperantly You have to raise the number, wanna try again?\n");
Console.WriteLine("If yes then type 'y'.\n");
A:
press = Console.ReadLine();
if (press.ToLower() == "y")
{
Console.WriteLine("\nEnter your new guess\n");
goto start2;
}
else
{
Console.WriteLine("\nInvalid character ! press 'y' to proceed\n");
goto A;
}

}
else if (UserNumber > PcNumber)
{
Console.WriteLine("\nYou were close. Gotta decrease the number a bit, wanna try again?\n");
Console.WriteLine("If yes then type 'y'.\n");
B:
press = Console.ReadLine();
if (press.ToLower() == "y")
{
Console.WriteLine("\nEnter your new guess\n");
goto start2;
}
else
{
Console.WriteLine("\nInvalid character ! press 'y' to proceed\n");
goto B;
}
}

Console.ReadKey();



2015-11-29
Aaron comment

 Aaron

I have a problem with using an int to continue. Basically the way it's done if someone enters a character it will break. But for the purpose of teaching Random then this is great :)


2015-06-28



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.