Skip to content

Commit

Permalink
Migrate math/rand to math/rand/v2
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Westra <[email protected]>
  • Loading branch information
webstradev committed Mar 2, 2025
1 parent 96c4edf commit f838578
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
6 changes: 4 additions & 2 deletions ulid.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"io"
"math"
"math/bits"
"math/rand"
"math/rand/v2"
"sync"
"time"
)
Expand Down Expand Up @@ -133,7 +133,9 @@ func MustNewDefault(t time.Time) ULID {
}

var defaultEntropy = func() io.Reader {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
seed := [32]byte{}
binary.LittleEndian.PutUint64(seed[:8], uint64(time.Now().UnixNano()))
rng := rand.NewChaCha8(seed)
return &LockedMonotonicReader{MonotonicReader: Monotonic(rng, 0)}
}()

Expand Down
31 changes: 19 additions & 12 deletions ulid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ package ulid_test
import (
"bytes"
crand "crypto/rand"
"encoding/binary"
"fmt"
"io"
"math"
"math/rand"
"math/rand/v2"
"strings"
"testing"
"testing/iotest"
Expand All @@ -31,9 +32,11 @@ import (

func ExampleULID() {
t := time.Unix(1000000, 0)
entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
rng := createChaCha8RNG(t)

entropy := ulid.Monotonic(rng, 0)
fmt.Println(ulid.MustNew(ulid.Timestamp(t), entropy))
// Output: 0000XSNJG0MQJHBF4QX1EFD6Y3
// Output: 0000XSNJG02VZD8JHMTREXTNAH
}

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -418,9 +421,8 @@ func TestULIDTime(t *testing.T) {
t.Errorf("got err %v, want %v", got, want)
}

rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 1e6; i++ {
ms := uint64(rng.Int63n(int64(maxTime)))
ms := uint64(rand.Int64N(int64(maxTime)))

var id ulid.ULID
if err := id.SetTime(ms); err != nil {
Expand Down Expand Up @@ -587,13 +589,12 @@ func TestScan(t *testing.T) {
}

func TestMonotonic(t *testing.T) {
now := ulid.Now()
for _, e := range []struct {
name string
mk func() io.Reader
}{
{"cryptorand", func() io.Reader { return crand.Reader }},
{"mathrand", func() io.Reader { return rand.New(rand.NewSource(int64(now))) }},
{"mathrand", func() io.Reader { return createChaCha8RNG(time.Now()) }},
} {
for _, inc := range []uint64{
0,
Expand Down Expand Up @@ -655,7 +656,7 @@ func TestMonotonicSafe(t *testing.T) {
t.Parallel()

var (
rng = rand.New(rand.NewSource(time.Now().UnixNano()))
rng = createChaCha8RNG(time.Now())
safe = &ulid.LockedMonotonicReader{MonotonicReader: ulid.Monotonic(rng, 0)}
t0 = ulid.Timestamp(time.Now())
)
Expand Down Expand Up @@ -689,7 +690,7 @@ func TestMonotonicSafe(t *testing.T) {

func TestULID_Bytes(t *testing.T) {
tt := time.Unix(1000000, 0)
entropy := ulid.Monotonic(rand.New(rand.NewSource(tt.UnixNano())), 0)
entropy := ulid.Monotonic(createChaCha8RNG(tt), 0)
id := ulid.MustNew(ulid.Timestamp(tt), entropy)
bid := id.Bytes()
bid[len(bid)-1]++
Expand All @@ -698,6 +699,12 @@ func TestULID_Bytes(t *testing.T) {
}
}

func createChaCha8RNG(t time.Time) *rand.ChaCha8 {
seed := [32]byte{}
binary.LittleEndian.PutUint64(seed[:8], uint64(t.UnixNano()))
return rand.NewChaCha8(seed)
}

func BenchmarkNew(b *testing.B) {
benchmarkMakeULID(b, func(timestamp uint64, entropy io.Reader) {
_, _ = ulid.New(timestamp, entropy)
Expand All @@ -714,7 +721,7 @@ func benchmarkMakeULID(b *testing.B, f func(uint64, io.Reader)) {
b.ReportAllocs()
b.SetBytes(int64(len(ulid.ULID{})))

rng := rand.New(rand.NewSource(time.Now().UnixNano()))
rng := createChaCha8RNG(time.Now())

for _, tc := range []struct {
name string
Expand Down Expand Up @@ -768,7 +775,7 @@ func BenchmarkMustParse(b *testing.B) {
}

func BenchmarkString(b *testing.B) {
entropy := rand.New(rand.NewSource(time.Now().UnixNano()))
entropy := createChaCha8RNG(time.Now())
id := ulid.MustNew(123456, entropy)
b.SetBytes(int64(len(id)))
b.ResetTimer()
Expand All @@ -778,7 +785,7 @@ func BenchmarkString(b *testing.B) {
}

func BenchmarkMarshal(b *testing.B) {
entropy := rand.New(rand.NewSource(time.Now().UnixNano()))
entropy := createChaCha8RNG(time.Now())
buf := make([]byte, ulid.EncodedSize)
id := ulid.MustNew(123456, entropy)

Expand Down

0 comments on commit f838578

Please sign in to comment.