Golang : Gonum standard normal random numbers example
Putting this simple example here for my own future reference. Just another way of generating random numbers beside using math/rand
.
In the code example below, we learn how to generate random numbers using the gonum.org/v1/gonum/stat/distuv
package.
Here you go!
package main
import (
"fmt"
"gonum.org/v1/gonum/stat/distuv"
)
func main() {
n := 1000 // number of simulations
// Create a standard normal (mean = 0, stdev = 1)
// https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.standard_normal.html
//dist := distuv.Normal{
// Mu: 0, // Mean of the normal distribution
// Sigma: 1, // Standard deviation of the normal distribution
//}
// use the defined variable
dist := distuv.UnitNormal.
z := make([]float64, n)
// Generate some random numbers from standard normal distribution
for i := range z {
z[i] = dist.Rand()
}
fmt.Println(z)
}
Reference :
See also : Golang : Create matrix with Gonum Matrix package example
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+20.8k Golang : Read directory content with os.Open
+8.2k Golang : Variadic function arguments sanity check example
+14.6k Golang : Rename directory
+14.3k Golang : Chunk split or divide a string into smaller chunk example
+17.4k Golang : How to tell if a file is compressed either gzip or zip ?
+15.2k Golang : package is not in GOROOT during compilation
+9.3k Golang : Generate random Chinese, Japanese, Korean and other runes
+33.1k Delete a directory in Go
+9.8k Golang : Qt get screen resolution and display on center example
+14.4k Golang : Get uploaded file name or access uploaded files
+4.7k Javascript : Access JSON data example
+42.1k Golang : How do I convert int to uint8?