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
+15.1k Golang : How to get own program name during runtime ?
+15.9k Golang : Padding data for encryption and un-padding data for decryption
+5.5k Android Studio : AlertDialog to get user attention example
+10k Golang : How to parse plain email text and process email header?
+4.3k Golang : Scan forex opportunities by Bollinger bands
+6.5k Golang : Intercept and compare HTTP response code example
+5.1k Golang : How to handle file size larger than available memory panic issue
+3.4k Javascript : How to get width and height of a div?
+17.5k Golang : Delete item from slice based on index/key position
+5.4k Android Studio : How to detect camera, activate and capture example
+4.3k Fix fatal error: evacuation not done in time problem
+3.6k Responsive Google Adsense