5.3 Generate a Random Number

Now that the player has chosen a number, the computer will generate a random one.

The GameUtilities include a function to help with that:

๐Ÿ’ป C#

// Generates a number between 1 and 10.
GameRandom.Number(1, 10);

It generates a random number within the range passed in the arguments.


Like we did with the player number, we need to store it in a variable and print it in the console.


โœ… What to Do

๐ŸŽฏ Expected Outcome

๐Ÿ–จ๏ธ Console

+--------------------------------+
+-- Welcome to Odds and Evens! --+
+--------------------------------+


--> Choose 1 for Odds or 2 for Evens:
1

# You chose Odds
# Computer is Evens

--> Choose a number between 1 and 2:
1

# You chose: 1
# Computer chose: 2

๐Ÿ™ˆ Solution
Tried, you must, before reveal the solution you may. - Yoda

โœ๏ธ Program.cs - Changes

    // Computer randomly generates a number between 1 and 2.
++  int computerNumber = GameRandom.Number(1, 2);
++  GamePrint.Message("Computer chose: " + computerNumber);

๐Ÿ—’๏ธ Program.cs - Final

(...)

// Computer randomly generates a number between 1 and 2.
int computerNumber = GameRandom.Number(1, 2);
GamePrint.Message("Computer chose: " + computerNumber);

(...)