首页 > 其他分享 >Zgo - randInt, randString

Zgo - randInt, randString

时间:2024-06-10 17:43:35浏览次数:19  
标签:randInt int randString func Zgo ASCII 94

 

package main

import (
    "fmt"
    "math/rand"
    "strings"
)

const (
    // As we only want to get printable ASCII characters, we limit the range of pseudo-random numbers
    // that can be generated. The total number of printable characters in the ASCII table is 94. This means
    // that the range of the pseudo-random numbers that the program can generate should be from 0
    // to 94, without including 94. Therefore, the values of the MIN and MAX global variables, 
    // are 0 and 94, respectively.
    MIN = 0
    MAX = 94
)

func main() {
    fmt.Println(randString(20))
}

func randInt(min, max int) int {
    // Intn returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n) from the default [Source]. It panics if n <= 0.
    return rand.Intn(max - min) + min
}

func randString(len int64) string {
    // The startChar variable holds the first ASCII character that can be generated by the utility, which,
    // in this case, is the exclamation mark, which has a decimal ASCII value of 33.
    startChar := "!"

    sb := strings.Builder{}
    var i int64 = 1
    for {
        myRand := randInt(MIN, MAX)
        newChar := string(startChar[0] + byte(myRand))
        sb.WriteString(newChar)

        if i == len {
            break
        }
        i++
    }

    return sb.String()
}

 

标签:randInt,int,randString,func,Zgo,ASCII,94
From: https://www.cnblogs.com/zhangzhihui/p/18240849

相关文章

  • Zgo - custom_log.go
     packagemainimport("fmt""io""log""os""path")funcmain(){flag:=os.O_APPEND|os.O_CREATE|os.O_WRONLYlogFile:=path.Join(os.TempDir(),"mGo.log")......
  • Zgo - stats.go
     packagemainimport("fmt""math""os""slices""strconv")funcmain(){args:=os.Argsiflen(args)==1{fmt.Println("Needoneormorearguments!")......
  • Zgo - which.go
     packagemainimport("fmt""os""path/filepath")funcmain(){args:=os.Argsiflen(args)==1{fmt.Println("Pleaseprovideanargument!")return}file:=args......
  • 讲解mtrand.RandomState.randint low >= high
    讲解mtrand.RandomState.randint(low>=high)的问题在使用NumPy进行随机数生成时,我们常常会使用mtrand.RandomState.randint(low,high)函数来生成指定范围内的随机整数。然而,在使用这个函数时,有一个非常容易犯错的地方,就是将low参数设置大于或等于high参数。让我们来看......
  • pytorch使用(四)np.random.randint用法
    np.random.randint用法np.random.randint是numpy库中用于生成随机整数的函数。它的用法如下:numpy.random.randint(low,high=None,size=None,dtype='l')其中,各个参数的含义如下:low:生成的随机整数的下限(包含)。high:生成的随机整数的上限(不包含)。如果不提供high参数,则生......
  • Numpy之random.randint产生随机整数
    前言本文主要讲述了如何使用Numpy的random.randint来产生随机整数,我们演示了如何生成不同上限或下限的指定大小的数组方法numpy.random.randint(low,high=None,s......
  • random.randint(min, max)
    python中生成区间随机数的时候,使用的 random.randint(min,max)居然是包括最大数的,又被坑了......
  • python报错 'int' object has no attribute 'randint'
    先看我的代码importrandom##随机生成[1,10)步长为2random=random.randrange(1,10,2)print(random)r=random.randint(0,10)print(r)print(random.randint(......
  • np.random.randint()的用法
      函数的作用是,返回一个随机整型数,其范围为[low,high)。如果没有写参数high的值,则返回[0,low)的值。从random可以看出是产生随机数,randint可以看出是产生随机整数(in......