Go Random Numbers
Go provides two main packages for generating random numbers: math/rand for general use and crypto/rand for security-sensitive tasks.
1. General Random Numbers (math/rand)
By default, math/rand is deterministic (it produces the same numbers every time). You should seed it to get different results.
2. Shuffling Slices
A common task is to shuffle a list of items.
3. Secure Random Numbers (crypto/rand)
Use this for passwords, tokens, or encryption keys. It is much slower but safe from prediction.
Why use Random Numbers?
- Simulations: Modeling real-world events.
- Security: Generating unique IDs or tokens.
- Testing: Fuzzing your code with random inputs.
- Games: Dice rolls, card shuffling, or spawning enemies.