Source File
math.go
Belonging Package
github.com/pion/randutil
package randutilimport (mrand // used for non-crypto unique ID and random port selection)// MathRandomGenerator is a random generator for non-crypto usage.type MathRandomGenerator interface {// Intn returns random integer within [0:n).Intn(n int) int// Uint32 returns random 32-bit unsigned integer.Uint32() uint32// Uint64 returns random 64-bit unsigned integer.Uint64() uint64// GenerateString returns ranom string using given set of runes.// It can be used for generating unique ID to avoid name collision.//// Caution: DO NOT use this for cryptographic usage.GenerateString(n int, runes string) string}type mathRandomGenerator struct {r *mrand.Randmu sync.Mutex}// NewMathRandomGenerator creates new mathmatical random generator.// Random generator is seeded by crypto random.func () MathRandomGenerator {, := CryptoUint64()if != nil {// crypto/rand is unavailable. Fallback to seed by time.= uint64(time.Now().UnixNano())}return &mathRandomGenerator{r: mrand.New(mrand.NewSource(int64()))}}func ( *mathRandomGenerator) ( int) int {.mu.Lock():= .r.Intn().mu.Unlock()return}func ( *mathRandomGenerator) () uint32 {.mu.Lock():= .r.Uint32().mu.Unlock()return}func ( *mathRandomGenerator) () uint64 {.mu.Lock():= .r.Uint64().mu.Unlock()return}func ( *mathRandomGenerator) ( int, string) string {:= []rune():= make([]rune, )for := range {[] = [.Intn(len())]}return string()}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |