欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VB.NET生成随机串或随机数字的方法总结

程序员文章站 2023-11-22 10:38:58
本文一共介绍了5种生成随机数方式,具体如下: 第一种:转载的方法 public enum stringtype allstring = 1...

本文一共介绍了5种生成随机数方式,具体如下:

第一种:转载的方法

public enum stringtype
    allstring = 1    '大小写字母
    allnumic = 2    '数字
    str_num = 3    '大小写字母+数字
    str_upper = 4    '大写字母
    str_lower = 5 '大写字母  
  end enum
  function generaterandom(byval length as integer, byval s as stringtype) as string
    dim strtemp as string = ""
    dim constant() as string = nothing
    select case s
      case stringtype.allnumic
        strtemp = "0,1,2,3,4,5,6,7,8,9"
        constant = strtemp.split(",")
      case stringtype.allstring
        strtemp = "a,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,d,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,x,y,z"
        constant = strtemp.split(",")
      case stringtype.str_lower
        strtemp = "a,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
        constant = strtemp.split(",")
      case stringtype.str_num
        strtemp = "a,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,d,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,x,y,z,0,1,2,3,4,5,6,7,8,9"
        constant = strtemp.split(",")
      case stringtype.str_upper
        strtemp = "a,b,c,e,f,d,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z"
        constant = strtemp.split(",")
    end select
    dim newrandom as system.text.stringbuilder = new system.text.stringbuilder(length)
    dim rd as random = new random()
    dim i as integer
    for i = 0 to length - 1 step i + 1
      newrandom.append(constant(rd.next(constant.length - 1)))
    next
    return newrandom.tostring()
  end function

第二种:网上查找

public function getoauthnonce() as string ' 得到随机值
    dim result as string = system.guid.newguid().tostring()
    result = result.replace("-", "")
    return result.substring(0, 10)
  end function
public function randcode(byval n as integer) as string
    dim arrchar as char() = new char() {"a"c, "b"c, "d"c, "c"c, "e"c, "f"c, _
                      "g"c, "h"c, "i"c, "j"c, "k"c, "l"c, _
                      "m"c, "n"c, "p"c, "r"c, "q"c, "s"c, _
                      "t"c, "u"c, "v"c, "w"c, "z"c, "y"c, _
                      "x"c, "0"c, "1"c, "2"c, "3"c, "4"c, _
                      "5"c, "6"c, "7"c, "8"c, "9"c, "a"c, _
                      "b"c, "c"c, "d"c, "e"c, "f"c, "g"c, _
                      "h"c, "i"c, "j"c, "k"c, "l"c, "m"c, _
                      "n"c, "q"c, "p"c, "r"c, "t"c, "s"c, _
                      "v"c, "u"c, "w"c, "x"c, "y"c, "z"c}
    dim num as new stringbuilder()
    dim rnd as new random(datetime.now.millisecond)
    for i as integer = 0 to n - 1
      num.append(arrchar(rnd.[next](0, arrchar.length)).tostring())
    next
    return num.tostring()
  end function

第三种:原来是c#转换

public function randcode(byval n as integer) as string
    dim arrchar as char() = new char() {"a"c, "b"c, "d"c, "c"c, "e"c, "f"c, _
                      "g"c, "h"c, "i"c, "j"c, "k"c, "l"c, _
                      "m"c, "n"c, "p"c, "r"c, "q"c, "s"c, _
                      "t"c, "u"c, "v"c, "w"c, "z"c, "y"c, _
                      "x"c, "0"c, "1"c, "2"c, "3"c, "4"c, _
                      "5"c, "6"c, "7"c, "8"c, "9"c, "a"c, _
                      "b"c, "c"c, "d"c, "e"c, "f"c, "g"c, _
                      "h"c, "i"c, "j"c, "k"c, "l"c, "m"c, _
                      "n"c, "q"c, "p"c, "r"c, "t"c, "s"c, _
                      "v"c, "u"c, "w"c, "x"c, "y"c, "z"c}
    dim num as new stringbuilder()
    dim rnd as new random(datetime.now.millisecond)
    for i as integer = 0 to n - 1
      num.append(arrchar(rnd.[next](0, arrchar.length)).tostring())
    next
    return num.tostring()
  end function

第四种:利用vb6转换

function makerand(byval maxlen as integer) as string '生成签名时用随机串
    dim strnewpass as string = vbnullstring
    dim lower as long
    dim whatsnext as long
    dim upper as long
    dim intcounter as long
    randomize()
    for intcounter = 1 to maxlen
      whatsnext = int((1 - 0 + 1) * rnd() + 0)
      if whatsnext = 0 then
        upper = 122
        lower = 100
      else
        upper = 57
        lower = 48
      end if
      strnewpass = strnewpass & chr(int((upper - lower + 1) * rnd() + lower))
    next
    makerand = strnewpass
  end function

第五种:直接用vb.net函数

  dim rand as random = new system.random(10)‘这里10就代表是10为
   debug.print(rand.next().tostring)

原文链接:http://blog.csdn.net/lcp58006478/article/details/8958460

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。