English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base en Golang

Instructions de contrôle en Golang

Fonction & Méthode en Golang

Structure en Golang

Coupe & Tableau en Golang

Chaîne (String) en Golang

Pointeur en Golang

Interface en Golang

Concurrence en Golang

Exception (Error) de Golang

Autres éléments de Golang

Go Language Random Number Generation (rand)

Nous pouvons utiliser l'objet rand pour générer des nombres aléatoires. Nous devons fournir quelques graines à l'objet rand pour que les nombres générés soient différents. Si nous ne fournissons pas de graines, le compilateur produira toujours le même résultat.

Génération de nombres aléatoires

package main
import \
import (
	"math"/rand"
	//"time"
	"time"
)
func main() {
	fmt.Print(rand.Intn(100))  //produira 0 à100 entre des entiers aléatoires
	fmt.Println()
	fmt.Print(rand.Float64())	//produira 0 à1un nombre aléatoire entre
	fmt.Println()
	rand.Seed(time.Now().Unix())  //Les nombres aléatoires générés par Seed
	myrand := random(1, 20)
	fmt.Println(myrand)
}
func random(min, max int) int {
	retourne rand.Intn(max - min) + min
}

Sortie :

81
0.9405090880450124
17