Randomness lets programs produce outcomes determined by chance rather than fully predictable from available information. Games use it to create surprise, variety, and risk. section 10.0 section 10.2
Python provides a built-in random module. Use randint for an inclusive integer range, such as 0 through 9 for code digits or 1 through 6 for dice; use random for a floating-point value between 0 and 1. section 10.2
```pythonimport random# Generate a random integer between 1 and 10 (inclusive)random_number = random.randint(1, 10)print(random_number)# Generate a random float between 0 and 1random_float = random.random()print(random_float)```
Use the short Python listing to compare integer and floating-point random values. section 10.2
Randomness supports several approachable game mechanics:
- Generate a secret code that another player must deduce in Bulls and Cows. section 10.3
- Roll virtual dice in Bogart, where players balance risk and luck while pursuing a target score. section 10.0 section 10.4
- Vary enemy behavior, puzzle orientation, or attack damage to make game situations less predictable. section 10.2
Randomness also describes a limitation of large language models: their responses can vary or be wrong because natural-language interaction lacks the exact specification and deterministic behavior of a compiler. section 12.2.2




