The name 'random' does not exist in the current context

mystery_xD

New member
Joined
Dec 7, 2021
Messages
1
Programming Experience
Beginner
Been trying to make this work, no idea what to do.

Heres my code.
C#:
using System;
namespace numbers
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = random.Next(1,1000);

            Console.Write("Welcome! I have chosen a number between 1 and 1000. Want to take a guess? ");

            int guess = Convert.ToInt32(Console.Read());

            if (guess < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
           
            int guess2 = Convert.ToInt32(Console.Read());
            if (guess2 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess2 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess2 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess3 = Convert.ToInt32(Console.Read());
            if (guess3 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess3 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess3 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess4 = Convert.ToInt32(Console.Read());
            if (guess4 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess4 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess4 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess5 = Convert.ToInt32(Console.Read());
            if (guess5 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess5 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess5 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess6 = Convert.ToInt32(Console.Read());
            if (guess6 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess6 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess6 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess7 = Convert.ToInt32(Console.Read());
            if (guess7 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess7 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess7 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess8 = Convert.ToInt32(Console.Read());
            if (guess8 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess8 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess8 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess9 = Convert.ToInt32(Console.Read());
            if (guess9 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess9 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess9 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess10 = Convert.ToInt32(Console.Read());
            if (guess10 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess10 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess10 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess11 = Convert.ToInt32(Console.Read());
            if (guess11 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess11 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess11 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess12 = Convert.ToInt32(Console.Read());
            if (guess12 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess12 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess12 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess13 = Convert.ToInt32(Console.Read());
            if (guess13 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess13 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess13 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess14 = Convert.ToInt32(Console.Read());
            if (guess14 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess14 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess14 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess15 = Convert.ToInt32(Console.Read());
            if (guess15 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess15 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess15 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess16 = Convert.ToInt32(Console.Read());
            if (guess16 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess16 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess16 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess17 = Convert.ToInt32(Console.Read());
            if (guess17 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess17 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess17 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess18 = Convert.ToInt32(Console.Read());
            if (guess18 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess18 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess18 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess19 = Convert.ToInt32(Console.Read());
            if (guess19 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess19 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess19 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }
            int guess20 = Convert.ToInt32(Console.Read());
            if (guess20 < num)
            {
                Console.Write("My number is lower.");
            }
            else if (guess20 > num)
            {
                Console.Write("My number is higher");
            }
            else if (guess20 == num)
            {
                Console.Write("You guessed my number! It was " + num + "!");
            }


           
        }
    }
}
 
Last edited by a moderator:
As an aside, if you notice yourself writing repetitive code, it's a code smell indicating that you should look at implementing loops and/or helper methods/functions.
 
For future reference:
  • Don't make us wade through hundreds of lines of code to find where there might be a problem. Post only the code relevant to the issue and, if a particular line generates an error message, point out that line. The code formatting that the editor provides will also let you highlight one or more lines of the formatted code, which is another reason to use it.
  • Provide a FULL and CLEAR explanation of the problem. We should have to work out what you're trying to do from code that doesn't do it. Tell us EXACTLY what you're trying to achieve, how you're trying to achieve it and what happens when you try.
If you would like us to volunteer our time and effort to help you, you should take the time and effort to help us do so. Sometimes it will easy enough regardless but many times it will not. Help us to help you and everyone will be happy.
 

Latest posts

Back
Top Bottom