-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #553 from SoulPancake/ab/math-rand-v2-mgr
feat: Migrate to math/rand/v2 Signed-off-by: Rueian <[email protected]>
- Loading branch information
Showing
11 changed files
with
113 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build go1.22 | ||
// +build go1.22 | ||
|
||
package util | ||
|
||
import ( | ||
"encoding/binary" | ||
"math/rand/v2" | ||
) | ||
|
||
func Shuffle(n int, swap func(i, j int)) { | ||
rand.Shuffle(n, swap) | ||
} | ||
|
||
func FastRand(n int) int { | ||
return rand.IntN(n) | ||
} | ||
|
||
func RandomBytes() []byte { | ||
val := make([]byte, 24) | ||
binary.BigEndian.PutUint64(val[0:8], rand.Uint64()) | ||
binary.BigEndian.PutUint64(val[8:16], rand.Uint64()) | ||
binary.BigEndian.PutUint64(val[16:24], rand.Uint64()) | ||
return val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//go:build !go1.22 | ||
// +build !go1.22 | ||
|
||
package util | ||
|
||
import ( | ||
"encoding/binary" | ||
"math/rand" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var sources sync.Pool | ||
|
||
func init() { | ||
sources = sync.Pool{New: func() any { return rand.New(rand.NewSource(time.Now().UnixNano())) }} | ||
} | ||
|
||
func Shuffle(n int, swap func(i, j int)) { | ||
rand.Shuffle(n, swap) | ||
} | ||
|
||
var rngPool = sync.Pool{ | ||
New: func() any { | ||
return rand.New(rand.NewSource(time.Now().UnixNano())) | ||
}, | ||
} | ||
|
||
func FastRand(n int) (r int) { | ||
s := rngPool.Get().(*rand.Rand) | ||
r = s.Intn(n) | ||
rngPool.Put(s) | ||
return | ||
} | ||
|
||
func RandomBytes() []byte { | ||
val := make([]byte, 24) | ||
src := sources.Get().(rand.Source64) | ||
binary.BigEndian.PutUint64(val[0:8], src.Uint64()) | ||
binary.BigEndian.PutUint64(val[8:16], src.Uint64()) | ||
binary.BigEndian.PutUint64(val[16:24], src.Uint64()) | ||
sources.Put(src) | ||
return val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestShuffle(t *testing.T) { | ||
arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} | ||
Shuffle(len(arr), func(i, j int) { | ||
arr[i], arr[j] = arr[j], arr[i] | ||
}) | ||
if len(arr) != 10 { | ||
t.Errorf("Expected array length 10, got %d", len(arr)) | ||
} | ||
} | ||
|
||
func TestFastRand(t *testing.T) { | ||
n := 10 | ||
res := FastRand(n) | ||
if res < 0 || res >= n { | ||
t.Errorf("Expected result between 0 and %d, got %d", n-1, res) | ||
} | ||
} | ||
|
||
func TestRandomBytes(t *testing.T) { | ||
val := RandomBytes() | ||
if len(val) != 24 { | ||
t.Errorf("Expected length 24, got %d", len(val)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters