Method of generating random Numbers in Go language

  • 2020-05-12 02:46:38
  • OfStack

This article demonstrates an example of how the Go language generates random Numbers. Share with you for your reference. The specific implementation method is as follows:

golang generates random Numbers using the math/rand package

package main
      
import (
    "fmt"
    "math/rand"
)
      
func main() {
    for i:=0; i<10; i++ {
        fmt.Println(rand.Intn(100))
    }
}

Find this situation, each time the results of the implementation of 1, not satisfied

package main
   
import (
    "fmt"
    "time"
    "math/rand"
)
   
func main() {
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    for i:=0; i<10; i++ {
        fmt.Println(r.Intn(100))
    }
}

This way you can use time seeds to get different results

I hope this article has helped you with the programming of Go language.


Related articles: