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
+33.9k Golang : convert(cast) bytes to string
+15.4k Golang : How to check if IP address is in range
+21.5k Golang : How to force compile or remove object files first before rebuild?
+5.1k Python : Convert(cast) bytes to string example
+23.1k Golang : Calculate time different
+9.9k Golang : Qt get screen resolution and display on center example
+12.9k Golang : flag provided but not defined error
+15.2k Golang : Search folders for file recursively with wildcard support
+8.3k Android Studio : Rating bar example
+8.9k Golang : Gorilla web tool kit schema example
+17.7k Golang : Clone with pointer and modify value