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
+12.1k Golang : Search and extract certain XML data example
+5.1k Golang : Generate Interleaved 2 inch by 5 inch barcode
+4.5k Linux/MacOSX : How to symlink a file?
+5.1k Swift : Convert string array to array example
+21.3k SSL : How to check if current certificate is sha1 or sha2
+5.1k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+11.6k Golang : Convert(cast) bigint to string
+5.2k Golang : What is StructTag and how to get StructTag's value?
+10.6k Nginx : TLS 1.2 support
+32k Golang : Copy directory - including sub-directories and files
+5.7k Facebook : How to force facebook to scrape latest URL link data?
+11.2k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions