What You’ll Build
A command-line game that:- Accepts a number guess from the user
- Generates random numbers multiple times
- Checks if any random number matches the guess
- Declares the player a winner if they match
- Uses proper randomization seeding
What You’ll Learn
- Generating random numbers with
math/rand - Seeding random number generators
- Converting strings to integers with
strconv - Using loops for game logic
- Validating user input
- Early returns for error handling
Game Rules
- Player provides a number as a command-line argument
- The program generates 5 random numbers between 0 and the player’s number
- If any random number matches the guess, the player wins
- The larger the number, the harder the game
Understanding Randomization
First, let’s understand how random number generation works:Complete Game Implementation
Running the Game
Key Concepts
Random Number Generation
Random Number Generation
Seeding the Random Generator
Seeding the Random Generator
UnixNano() returns current time in nanoseconds since Jan 1, 1970.String to Integer Conversion
String to Integer Conversion
Early Returns
Early Returns
Game Mechanics Explained
Probability Mathematics
Understanding the odds:Enhancements to Try
- Show attempts - Print each random number generated
- Difficulty levels - Easy (5 numbers), Medium (10), Hard (20)
- Score tracking - Count wins/losses across multiple games
- Range guessing - Guess within a range instead of exact match
- Hints - Show “higher” or “lower” hints
- Time limit - Add a time constraint
- Statistics - Track and display win rate
Debugging Version
Show what numbers are generated:Common Mistakes
Next Steps
Word Finder
More loop practice with strings
Loops Guide
Master Go loops