Skip to content

Commit

Permalink
Implement Rand (#51)
Browse files Browse the repository at this point in the history
Addresses issue #20

Co-authored-by: Galih Fajar <[email protected]>
  • Loading branch information
GalihFajar and Galih Fajar authored Aug 9, 2024
1 parent 9e7a7a8 commit 1923952
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package govec

import (
"math/rand"
"time"

"golang.org/x/exp/constraints"
)

// RandV2F returns a new random normalized V2F vector
func RandV2F[T constraints.Float]() V2F[T] {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return V2F[T]{T(r.Float32()), T(r.Float32())}.Norm()
}

// RandV3F returns a new random normalized V3F vector
func RandV3F[T constraints.Float]() V3F[T] {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return V3F[T]{T(r.Float32()), T(r.Float32()), T(r.Float32())}.Norm()
}
62 changes: 62 additions & 0 deletions rand_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package govec

import (
"math"
"testing"
)

func Test_RandV2F(t *testing.T) {
t.Run("generate 2 different random f32 vectors, shouldn't be equal", func(t *testing.T) {
v1 := RandV2F[float32]()
v2 := RandV2F[float32]()

if v1.X == v2.X || v1.Y == v2.Y || v1.X == v1.Y || v2.X == v2.Y {
t.Errorf("Expected [%f, %f] not equal to each other or with [%f, %f]", v1.X, v1.Y, v2.X, v2.Y)
}
})

t.Run("generate 2 different random f64 vectors, shouldn't be equal", func(t *testing.T) {
v1 := RandV2F[float64]()
v2 := RandV2F[float64]()

if v1.X == v2.X || v1.Y == v2.Y || v1.X == v1.Y || v2.X == v2.Y {
t.Errorf("Expected [%f, %f] not equal to each other or with [%f, %f]", v1.X, v1.Y, v2.X, v2.Y)
}
})

t.Run("randomized vectors should be normalized", func(t *testing.T) {
v := RandV2F[float32]()

if !almostEqual[float64](math.Sqrt(float64((v.X*v.X)+(v.Y*v.Y))), 1.0, 1e-5) {
t.Errorf("Expected [%f, %f] to be normalized!", v.X, v.Y)
}
})
}

func Test_RandV3F(t *testing.T) {
t.Run("generate 2 different random f32 vectors, shouldn't be equal", func(t *testing.T) {
v1 := RandV3F[float32]()
v2 := RandV3F[float32]()

if v1.X == v2.X || v1.Y == v2.Y || v1.Z == v2.Z || v1.X == v1.Y || v1.X == v1.Z || v1.Y == v1.Z || v2.X == v2.Y || v2.X == v2.Z || v2.Y == v2.Z {
t.Errorf("Expected [%f, %f, %f] not equal to each other or with [%f, %f, %f]", v1.X, v1.Y, v1.Z, v2.X, v2.Y, v2.Z)
}
})

t.Run("generate 2 different random f64 vectors, shouldn't be equal", func(t *testing.T) {
v1 := RandV3F[float64]()
v2 := RandV3F[float64]()

if v1.X == v2.X || v1.Y == v2.Y || v1.Z == v2.Z || v1.X == v1.Y || v1.X == v1.Z || v1.Y == v1.Z || v2.X == v2.Y || v2.X == v2.Z || v2.Y == v2.Z {
t.Errorf("Expected [%f, %f, %f] not equal to each other or with [%f, %f, %f]", v1.X, v1.Y, v1.Z, v2.X, v2.Y, v2.Z)
}
})

t.Run("randomized vectors should be normalized", func(t *testing.T) {
v := RandV3F[float32]()

if !almostEqual[float64](math.Sqrt(float64((v.X*v.X)+(v.Y*v.Y)+(v.Z*v.Z))), 1.0, 1e-5) {
t.Errorf("Expected [%f, %f, %f] to be normalized!", v.X, v.Y, v.Z)
}
})
}

0 comments on commit 1923952

Please sign in to comment.