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
+5.3k Swift : Convert string array to array example
+4.9k Python : Convert(cast) bytes to string example
+5.4k Golang : Display advertisement images or strings on random order
+7.6k Golang : Mapping Iban to Dunging alphabets
+8.9k Golang : automatically figure out array length(size) with three dots
+13.9k Golang : convert rune to unicode hexadecimal value and back to rune character
+16.8k Golang : Set up source IP address before making HTTP request
+12.2k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+7.6k Golang : Error reading timestamp with GORM or SQL driver
+4.7k Facebook : How to place save to Facebook button on your website
+16.4k Golang : Test floating point numbers not-a-number and infinite example
+5.1k Golang : Experimental Jawi programming language