Nombres aléatoires
Contexte
Section intitulée « Contexte »Le package math/rand génère des nombres pseudo‑aléatoires. Pour un aléatoire cryptographique, utilisez crypto/rand. Depuis Go 1.20, le générateur global est initialisé automatiquement.
Générer des entiers, des flottants et des permutations aléatoires.
Code exemple
Section intitulée « Code exemple »package main
import ( "fmt" "math/rand")
func main() { fmt.Println(rand.Intn(100)) // 0-99 fmt.Println(rand.Float64()) // 0.0-1.0 fmt.Println(rand.Int()) // non négatif
// Permutation de [0..n) perm := rand.Perm(5) fmt.Println(perm)}Sortie (exemple)
Section intitulée « Sortie (exemple) »420.1234567891234567890123456789[2 0 4 1 3]