go 生成随机小数 指定范围
package main import ( "crypto/hmac" "crypto/sha1" "encoding/base64" "encoding/json" "errors" "fmt" "net/http" "net/url" "strings" "time" "math/rand" "strconv" . "github.com/nntaoli-project/GoEx" "github.com/sumorf/goalgo" "github.com/sumorf/goalgo/log" ) func main(){
//必须是位数相同,前面的也要相同(反正就是不好用,但是我上网搜了也搜不到,只是符合我的需求而已) RandFloat64(0.001234,0.009874) } //随机整数 func RandInt64(min, max int64) int64 { if min >= max || min == 0 || max == 0 { return max } rand.Seed(time.Now().UnixNano()) return rand.Int63n(max-min) + min } //zll 生成指定范围的随机小数,注:很不智能,所以限制min和max前面的数字必须相同例如min:0.00123456 max:0.00222336这样 func RandFloat64(min,max float64) float64{ //转为字符串 my_min := strconv.FormatFloat(min,'f',-1,64) my_max := strconv.FormatFloat(max,'f',-1,64) if len(my_min)!=len(my_max){ return 0 //fmt.Println("错误") }else{ qianzhui := "" houzhui1 := "" houzhui2 := "" sw := 0 for i := 0;i<len(my_min);i++ { str1 := string(my_min[i]) str2 := string(my_max[i]) if sw==0{ if my_min[i]==my_max[i]{ qianzhui = qianzhui + str1 }else{ sw = 1 houzhui1 = houzhui1 + str1 houzhui2 = houzhui2 + str2 } }else{ houzhui1 = houzhui1 + str1 houzhui2 = houzhui2 + str2 } } //return 0,qianzhui+"**"+houzhui1+"**"+houzhui2 //fmt.Println(qianzhui+"**"+houzhui1+"**"+houzhui2) //前缀字符串转整数 qian1,_ := strconv.ParseInt(houzhui1,10,64) qian2,_ := strconv.ParseInt(houzhui2,10,64) //生成随机整数 jj := RandInt64(qian1,qian2) //随机整数转字符串 jj_str := strconv.FormatInt(jj,10) //前缀拼接随机字符串 all_str := qianzhui+jj_str //全字符串转浮点数 jieguo,_ := strconv.ParseFloat(all_str,64) return jieguo } return 0 }
破罐子互摔