Skip to content

Commit f8c3e8c

Browse files
committed
random: add NewSource
math.Rand is the canonical way to generate pseudo-random numbers in Go. Allow integrating it with Antithesis randomness.
1 parent bd33a73 commit f8c3e8c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

random/random.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,37 @@
1212
package random
1313

1414
import (
15+
"math"
16+
"math/rand"
17+
1518
"github.com/antithesishq/antithesis-sdk-go/internal"
1619
)
1720

1821
// GetRandom returns a uint64 value chosen by Antithesis. You should not store this value or use it to seed a PRNG, but should use it immediately.
1922
func GetRandom() uint64 {
2023
return internal.Get_random()
2124
}
25+
26+
var _ rand.Source64 = source{}
27+
28+
type source struct{}
29+
30+
func (source) Seed(int64) {}
31+
32+
func (source) Int63() int64 {
33+
return int64(internal.Get_random() & (math.MaxUint64 >> 1))
34+
}
35+
36+
func (source) Uint64() uint64 {
37+
return internal.Get_random()
38+
}
39+
40+
// NewSource initialises a source of pseudo-random data.
41+
//
42+
// Use this function to create a [math/rand.Rand] which provides feedback to the Antithesis platform.
43+
// Outside of Antithesis this is equivalent to calling [math/rand.NewSource].
44+
//
45+
// The returned source implements [math/rand.Source64].
46+
func NewSource(seed int64) rand.Source {
47+
return source{}
48+
}

random/random_local.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ import (
99
func GetRandom() uint64 {
1010
return rand.Uint64()
1111
}
12+
13+
func NewSource(seed int64) rand.Source {
14+
return rand.NewSource(seed)
15+
}

0 commit comments

Comments
 (0)