Skip to content

Commit ee83631

Browse files
Use the default source for randomization (#177)
* Use the default source for randomization * Docstrings
1 parent 88aca14 commit ee83631

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

mathx/rand.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,24 @@ import (
55
"math/rand"
66
)
77

8-
// Random is a source of random values.
9-
type Random struct {
10-
Src *rand.Rand // Pseudo-random source seeded with a given value.
11-
}
12-
13-
// NewRandom returns a new Random that uses the provided seed to generate
14-
// random values.
15-
func NewRandom(seed int64) Random {
16-
src := rand.New(rand.NewSource(seed))
17-
return Random{Src: src}
18-
}
19-
208
// GetRandomInt returns a non-negative pseudo-random number in the interval [0, max).
219
// It returns 0 if max <= 0.
22-
func (r *Random) GetRandomInt(max int) int {
10+
// NOTE: this function uses the default Source from the math/rand package. This source
11+
// is only seeded with a random value since go 1.20.
12+
func GetRandomInt(max int) int {
2313
if max <= 0 {
2414
return 0
2515
}
26-
return r.Src.Intn(max)
16+
return rand.Intn(max)
2717
}
2818

2919
// GetExpDistributedInt returns a exponentially distributed number in the interval
3020
// [0, +math.MaxFloat64), rounded to the nearest int. Callers can adjust the rate of the
3121
// function through the rate parameter.
32-
func (r *Random) GetExpDistributedInt(rate float64) int {
33-
f := r.Src.ExpFloat64() / rate
22+
// NOTE: this function uses the default Source from the math/rand package. This source
23+
// is only seeded with a random value since go 1.20.
24+
func GetExpDistributedInt(rate float64) int {
25+
f := rand.ExpFloat64() / rate
3426
index := int(math.Round(f))
3527
return index
3628
}

mathx/rand_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package mathx
22

3-
import "testing"
3+
import (
4+
"math/rand"
5+
"testing"
6+
)
47

58
const seed = 1658340109320624211
69

7-
func TestRandom_GetRandomInt(t *testing.T) {
10+
func TestGetRandomInt(t *testing.T) {
811
tests := []struct {
912
name string
1013
max int
@@ -32,14 +35,14 @@ func TestRandom_GetRandomInt(t *testing.T) {
3235
}
3336
for _, tt := range tests {
3437
t.Run(tt.name, func(t *testing.T) {
35-
r := NewRandom(seed)
36-
got := r.GetRandomInt(tt.max)
38+
rand.Seed(seed)
39+
got := GetRandomInt(tt.max)
3740

3841
if got != tt.expected1 {
3942
t.Errorf("GetRandomInt() = %d, want %d", got, tt.expected1)
4043
}
4144

42-
got = r.GetRandomInt(tt.max)
45+
got = GetRandomInt(tt.max)
4346

4447
if got != tt.expected2 {
4548
t.Errorf("GetRandomInt() = %d, want %d", got, tt.expected2)
@@ -48,7 +51,7 @@ func TestRandom_GetRandomInt(t *testing.T) {
4851
}
4952
}
5053

51-
func TestRandom_GetExpDistributedInt(t *testing.T) {
54+
func TestGetExpDistributedInt(t *testing.T) {
5255
tests := []struct {
5356
name string
5457
rate float64
@@ -76,14 +79,14 @@ func TestRandom_GetExpDistributedInt(t *testing.T) {
7679
}
7780
for _, tt := range tests {
7881
t.Run(tt.name, func(t *testing.T) {
79-
r := NewRandom(seed)
80-
got := r.GetExpDistributedInt(tt.rate)
82+
rand.Seed(seed)
83+
got := GetExpDistributedInt(tt.rate)
8184

8285
if got != tt.expected1 {
8386
t.Errorf("GetExpDistributedInt() = %d, want %d", got, tt.expected1)
8487
}
8588

86-
got = r.GetExpDistributedInt(tt.rate)
89+
got = GetExpDistributedInt(tt.rate)
8790

8891
if got != tt.expected2 {
8992
t.Errorf("GetExpDistributedInt() = %d, want %d", got, tt.expected2)

0 commit comments

Comments
 (0)