zoukankan      html  css  js  c++  java
  • 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 }

      

  • 相关阅读:
    Datagrip导入导出为一个sql文件详细说明 (mysql)
    Linux/Unix/Mac OS下的远程访问和文件共享方式
    批量杀掉多个pid文件中记录的pid进程, 并集成到shell脚本中
    把tomcat服务器配置为windows服务的方法
    idea导入java项目
    linux-umount挂载点无法卸载:device is busy(解决)
    elasticsearch插件大全
    分布式搜索elasticsearch配置文件详解
    centos fastdfs 多服务器 多硬盘 多组 配置详解
    redis 配置 linux
  • 原文地址:https://www.cnblogs.com/zonglonglong/p/10038601.html
Copyright © 2011-2022 走看看