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.9k Golang : Convert PNG transparent background image to JPG or JPEG image
+20.3k Golang : Check if os.Stdin input data is piped or from terminal
+5.1k Golang : Display packages names during compilation
+5.4k Python : Delay with time.sleep() function example
+5.8k List of Golang XML tutorials
+9.5k Golang : Changing a RGBA image number of channels with OpenCV
+15.6k Golang : Force download file example
+18.3k Golang : Get path name to current directory or folder
+5.7k Golang : Frobnicate or tweaking a string example
+30.5k Get client IP Address in Go
+10.2k Golang : Text file editor (accept input from screen and save to file)
+9.3k Golang : How to check if a string with spaces in between is numeric?