get a normally distributed random number in go

To generate a normally distributed random number in Go, you can use the rand.NormFloat64() function from the math/rand package. This function returns a random float64 value drawn from the standard normal distribution with mean 0 and variance 1.

main.go
package main

import (
    "fmt"
    "math/rand"
)

func main() {
    // Seed the random number generator to ensure different sequence every time
    rand.Seed(123)

    // Generate a normally distributed random number
    mean := 0.0
    stdDev := 1.0
    num := rand.NormFloat64()*stdDev + mean

    fmt.Println(num)
}
321 chars
19 lines

You can adjust the mean and standard deviation by multiplying and adding to the generated number accordingly.

gistlibby LogSnag