π Instructions#
Now that the player has chosen a number, the computer needs to pick one too. To keep the game fair, the computer’s number should be unpredictable β chosen at random.
Most languages have a built-in way to generate random numbers. You create a random number generator, then ask it for a number within a range:
Random random = new Random();
int number = random.Next(1, 3);new Random() creates the generator. random.Next(1, 3) returns a random integer β the first argument is inclusive, the second is exclusive, so this produces either 1 or 2.
π Learn More
β What to Do#
Generate a random number between 1 and 2, store it in an integer variable named computerNumber, and print Computer chose: followed by its value.
π― Expected Outcome#
The computer’s number changes each run. Both outputs below are valid:
Welcome to Odds and Evens!
Choose 1 for Odds or 2 for Evens:
1
You chose Odds.
Choose a number between 1 and 2:
1
You chose: 1
Computer chose: 1
Welcome to Odds and Evens!
Choose 1 for Odds or 2 for Evens:
1
You chose Odds.
Choose a number between 1 and 2:
1
You chose: 1
Computer chose: 2
π‘ Hints#
Hint 1
You need to create the random number generator before you can use it.
Hint 2
The generator has a method that returns a random integer within a range. Check the code example in the instructions for the exact range arguments to use.
Hint 3
Printing the computer’s number follows the same pattern as printing the player’s number in the previous lesson.
β οΈ Common Mistakes#
Wrong range arguments
The upper bound of the range is exclusive β random.Next(1, 2) only ever returns 1. To get either 1 or 2, use random.Next(1, 3).
Forgetting to create the generator first
Calling random.Next() without first declaring Random random = new Random() causes a compile error. The generator must be created before it can be used.
Creating a new Random inside a loop later
Creating new Random() inside a loop can produce the same number repeatedly because each instance is seeded with the current time. Create the generator once, outside any loop. This isn’t an issue yet but is worth knowing before loops are introduced.
Using the wrong variable name
The variable must be named computerNumber β it’s referenced by name in the lessons that follow.
π Solution#
Tried, you must, before reveal the solution you may.
Program.csChanges
// Computer randomly generates a number between 1 and 2.
+ Random random = new Random();
+ int computerNumber = random.Next(1, 3);
+ Console.WriteLine($"Computer chose: {computerNumber}");
Program.csFinal
Console.WriteLine("Welcome to Odds and Evens!");
// Player chooses to play with odds or evens.
Console.WriteLine("Choose 1 for Odds or 2 for Evens:");
string input = Console.ReadLine();
int playerChoice = int.Parse(input);
if (playerChoice == 1)
{
Console.WriteLine("You chose Odds.");
}
else
{
Console.WriteLine("You chose Evens.");
}
// Player enters a number between 1 and 2.
Console.WriteLine("Choose a number between 1 and 2:");
string rawNumber = Console.ReadLine();
int playerNumber = int.Parse(rawNumber);
Console.WriteLine($"You chose: {playerNumber}");
// Computer randomly generates a number between 1 and 2.
Random random = new Random();
int computerNumber = random.Next(1, 3);
Console.WriteLine($"Computer chose: {computerNumber}");
(...)