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
+4.9k Golang : Issue HTTP commands to server and port example
+7.3k Gogland : Single File versus Go Application Run Configurations
+8.5k Android Studio : Image button and button example
+18.6k Golang : How to make function callback or pass value from function as parameter?
+11.3k CodeIgniter : Import Linkedin data
+5.6k Golang : Find change in a combination of coins example
+15k Golang : Delete certain files in a directory
+9.2k Golang : Convert(cast) string to int64
+9k Golang : How to get username from email address
+6.1k Golang : Detect face in uploaded photo like GPlus
+6.4k Golang : Find the longest line of text example
+25.4k Golang : missing Mercurial command