|
|
@@ -3,6 +3,10 @@
|
|
|
package dice
|
|
|
|
|
|
import (
|
|
|
+ crand "crypto/rand"
|
|
|
+ "github.com/v2fly/v2ray-core/v5/common"
|
|
|
+ "io"
|
|
|
+ "math/big"
|
|
|
"math/rand"
|
|
|
"time"
|
|
|
)
|
|
|
@@ -15,6 +19,17 @@ func Roll(n int) int {
|
|
|
return rand.Intn(n)
|
|
|
}
|
|
|
|
|
|
+// RollWith returns a non-negative number between 0 (inclusive) and n (exclusive).
|
|
|
+// Use random as the random source, if read fails, it panics.
|
|
|
+func RollWith(n int, random io.Reader) int {
|
|
|
+ if n == 1 {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ mrand, err := crand.Int(random, big.NewInt(int64(n)))
|
|
|
+ common.Must(err)
|
|
|
+ return int(mrand.Int64())
|
|
|
+}
|
|
|
+
|
|
|
// Roll returns a non-negative number between 0 (inclusive) and n (exclusive).
|
|
|
func RollDeterministic(n int, seed int64) int {
|
|
|
if n == 1 {
|