Skip to content

Commit

Permalink
Update randg
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Oct 10, 2023
1 parent fdf7aa5 commit 452f726
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func Const(c float64) Matrix {
return [][]float64{{c}}
}

func rnd(s ...rand.Source) *rand.Rand {
// randg returns a pseudo-random number generator.
func randg(s ...rand.Source) *rand.Rand {
if len(s) == 0 {
s = append(s, rand.NewSource(time.Now().UnixNano()))
}
Expand All @@ -48,14 +49,16 @@ func rnd(s ...rand.Source) *rand.Rand {
// m, n is the dimension of the matrix.
// s is the source of the pseudo-random number.
func Rand(m, n int, s ...rand.Source) Matrix {
return F(Zero(m, n), func(_ float64) float64 { return rnd(s...).Float64() })
rng := randg(s...)
return F(Zero(m, n), func(_ float64) float64 { return rng.Float64() })
}

// Randn returns a matrix with elements that normally distributed float64 in the range [-math.MaxFloat64, +math.MaxFloat64] with standard normal distribution.
// m, n is the dimension of the matrix.
// s is the source of the pseudo-random number.
func Randn(m, n int, s ...rand.Source) Matrix {
return F(Zero(m, n), func(_ float64) float64 { return rnd(s...).NormFloat64() })
rng := randg(s...)
return F(Zero(m, n), func(_ float64) float64 { return rng.NormFloat64() })
}

func Shape(m Matrix) []int {
Expand Down

0 comments on commit 452f726

Please sign in to comment.