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.7k Elasticsearch : Shutdown a local node
+6.1k Web : How to see your website from different countries?
+13.8k Golang : Get all local users and print out their home directory, description and group id
+7.8k Golang : Find network service name from given port and protocol
+8.5k Facebook : Getting the friends list with PHP return JSON format
+10.4k Golang : GTK Input dialog box examples
+9.3k Golang : Allow Cross-Origin Resource Sharing request
+9.3k Golang : How to get quoted string into another string?
+17k Golang : Iterating Elements Over A List
+17.6k Golang : Populate dropdown with html/template example
+13.1k Golang : Recombine chunked files example
+6.1k Linux : How to fix Brother HL-1110 printing blank page problem