github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/utils/random/random.go (about)

     1  package random
     2  
     3  import (
     4  	"math/rand"
     5  	"strings"
     6  	"time"
     7  	"unsafe"
     8  )
     9  
    10  func GenStringOnAscii(maxBytes uint32) string {
    11  	nByte := int(FastRandN(maxBytes))
    12  	var sb strings.Builder
    13  	sb.Grow(nByte)
    14  	for i := 0; i < nByte; i++ {
    15  		sb.WriteByte(byte(FastRandN(26)) + 65)
    16  	}
    17  	return sb.String()
    18  }
    19  
    20  func GenStringsOnAscii(maxNStr, maxBytes uint32) []string {
    21  	nStr := int(FastRandN(maxNStr))
    22  	strs := make([]string, nStr)
    23  	for i := 0; i < nStr; i++ {
    24  		strs[i] = GenStringOnAscii(maxBytes)
    25  	}
    26  	return strs
    27  }
    28  
    29  func GenBytesOnAscii(maxBytes uint32) []byte {
    30  	str := GenStringOnAscii(maxBytes)
    31  	return *(*[]byte)(unsafe.Pointer(&str))
    32  }
    33  
    34  func GenSequenceNumberOnMathRand(nSeq int) []uint32 {
    35  	seq := make([]uint32, nSeq)
    36  	rand.Seed(time.Now().UnixNano())
    37  	for i := 0; i < nSeq; i++ {
    38  		seq[i] = rand.Uint32()
    39  	}
    40  	return seq
    41  }
    42  
    43  func GenSequenceNumberOnFastRand(nSeq int) []uint32 {
    44  	seq := make([]uint32, nSeq)
    45  	for i := 0; i < nSeq; i++ {
    46  		seq[i] = FastRand()
    47  	}
    48  	return seq
    49  }