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
+9.9k Golang : Read binary file into memory
+8.8k Golang : convert(cast) string to integer value
+21k Golang : Login(Authenticate) with Facebook example
+10.4k Golang : Get command line arguments
+8k Golang : Underscore or snake_case to camel case example
+10.4k Golang : Get input from keyboard
+7.3k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+7.2k Golang : Implement getters and setters
+9.3k Golang : Clean up null characters from input data
+5.2k Golang : Perform sanity checks on filename example
+8.8k Golang : Get number of CPU cores
+7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error