math/rand/v2 package provides pseudorandom number generation.
Random Integers
rand.IntN returns a random int n where 0 <= n < 100:
Random Floats
rand.Float64 returns a float64 f where 0.0 <= f < 1.0:
Custom Float Ranges
Generate random floats in other ranges, for example5.0 <= f < 10.0:
Seeded Random Numbers
For reproducible sequences, create a newrand.Source with a known seed:
NewPCG creates a PCG source that requires two uint64 seed numbers.Common Functions
IntN(n int) - Random integer
IntN(n int) - Random integer
Returns a random integer from 0 to n-1
Float64() - Random float
Float64() - Random float
Returns a random float64 from 0.0 to 1.0
NewPCG(seed1, seed2 uint64) - Create source
NewPCG(seed1, seed2 uint64) - Create source
Creates a new PCG random source with seeds
New(src Source) - Create generator
New(src Source) - Create generator
Creates a new random generator from a source
Use Cases
Testing
Generate random test data
Simulations
Model random events
Games
Random game mechanics
Sampling
Random data sampling