zoukankan      html  css  js  c++  java
  • golang random string

    package main
    
    import (
    	"math/rand"
    	"strings"
    	"testing"
    	"time"
    	"unsafe"
    )
    
    // Implementations
    
    func init() {
    	rand.Seed(time.Now().UnixNano())
    }
    
    var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    
    func RandStringRunes(n int) string {
    	b := make([]rune, n)
    	for i := range b {
    		b[i] = letterRunes[rand.Intn(len(letterRunes))]
    	}
    	return string(b)
    }
    
    const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    const (
    	letterIdxBits = 6                    // 6 bits to represent a letter index
    	letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
    	letterIdxMax  = 63 / letterIdxBits   // # of letter indices fitting in 63 bits
    )
    
    func RandStringBytes(n int) string {
    	b := make([]byte, n)
    	for i := range b {
    		b[i] = letterBytes[rand.Intn(len(letterBytes))]
    	}
    	return string(b)
    }
    
    func RandStringBytesRmndr(n int) string {
    	b := make([]byte, n)
    	for i := range b {
    		b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
    	}
    	return string(b)
    }
    
    func RandStringBytesMask(n int) string {
    	b := make([]byte, n)
    	for i := 0; i < n; {
    		if idx := int(rand.Int63() & letterIdxMask); idx < len(letterBytes) {
    			b[i] = letterBytes[idx]
    			i++
    		}
    	}
    	return string(b)
    }
    
    func RandStringBytesMaskImpr(n int) string {
    	b := make([]byte, n)
    	// A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!
    	for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
    		if remain == 0 {
    			cache, remain = rand.Int63(), letterIdxMax
    		}
    		if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
    			b[i] = letterBytes[idx]
    			i--
    		}
    		cache >>= letterIdxBits
    		remain--
    	}
    
    	return string(b)
    }
    
    var src = rand.NewSource(time.Now().UnixNano())
    
    func RandStringBytesMaskImprSrc(n int) string {
    	b := make([]byte, n)
    	// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
    	for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
    		if remain == 0 {
    			cache, remain = src.Int63(), letterIdxMax
    		}
    		if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
    			b[i] = letterBytes[idx]
    			i--
    		}
    		cache >>= letterIdxBits
    		remain--
    	}
    
    	return string(b)
    }
    
    func RandStringBytesMaskImprSrcSB(n int) string {
    	sb := strings.Builder{}
    	sb.Grow(n)
    	// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
    	for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
    		if remain == 0 {
    			cache, remain = src.Int63(), letterIdxMax
    		}
    		if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
    			sb.WriteByte(letterBytes[idx])
    			i--
    		}
    		cache >>= letterIdxBits
    		remain--
    	}
    
    	return sb.String()
    }
    
    func RandStringBytesMaskImprSrcUnsafe(n int) string {
    	b := make([]byte, n)
    	// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
    	for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
    		if remain == 0 {
    			cache, remain = src.Int63(), letterIdxMax
    		}
    		if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
    			b[i] = letterBytes[idx]
    			i--
    		}
    		cache >>= letterIdxBits
    		remain--
    	}
    
    	return *(*string)(unsafe.Pointer(&b))
    }
    
    // Benchmark functions
    
    const n = 16
    
    func BenchmarkRunes(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		RandStringRunes(n)
    	}
    }
    
    func BenchmarkBytes(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		RandStringBytes(n)
    	}
    }
    
    func BenchmarkBytesRmndr(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		RandStringBytesRmndr(n)
    	}
    }
    
    func BenchmarkBytesMask(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		RandStringBytesMask(n)
    	}
    }
    
    func BenchmarkBytesMaskImpr(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		RandStringBytesMaskImpr(n)
    	}
    }
    
    func BenchmarkBytesMaskImprSrc(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		RandStringBytesMaskImprSrc(n)
    	}
    }
    func BenchmarkBytesMaskImprSrcSB(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		RandStringBytesMaskImprSrcSB(n)
    	}
    }
    
    func BenchmarkBytesMaskImprSrcUnsafe(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		RandStringBytesMaskImprSrcUnsafe(n)
    	}
    }
    
    
  • 相关阅读:
    【BZOJ-4289】Tax 最短路 + 技巧建图
    【BZOJ-3895】取石子 记忆化搜索 + 博弈
    【BZOJ-4569】萌萌哒 ST表 + 并查集
    【BZOJ-3832】Rally 拓扑序 + 线段树 (神思路题!)
    【BZOJ-4213】贪吃蛇 有上下界的费用流
    【BZOJ-3122】随机数生成器 BSGS
    【BZOJ-2299】向量 裴蜀定理 + 最大公约数
    【BZOJ-1441】Min 裴蜀定理 + 最大公约数
    【BZOJ-2438】杀人游戏 Tarjan + 缩点 + 概率
    【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分
  • 原文地址:https://www.cnblogs.com/yzhch/p/14841708.html
Copyright © 2011-2022 走看看