时间:2021-05-26
本文一共介绍了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 FunctionPublic 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函数
原文链接:http://blog.csdn.net/lcp58006478/article/details/8958460
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
目录生成随机数字生成0到1之间的随机数生成指定范围内的随机数生成6位数字手机验证码生成标准正态分布随机数生成随机字符串生成固定长度的随机字符串生成可变长度的随机
目录生成随机数字生成0到1之间的随机数生成指定范围内的随机数生成6位数字手机验证码生成遵循正态分布的随机数生成随机字符串生成固定长度的随机字符串生成可变长度的随
random是用于生成随机数的,我们可以利用它随机生成数字或者选择字符串。•random.seed(x)改变随机数生成器的种子seed。一般不必特别
1、随机生成4位数的随机数/***随机生成4位的随机数*http://www.yulu.jb51.net*/document.write(parseInt(10
1,生成随机数用for循环确定生成几个随机数。用随机函数生成范围内随机数。例如rand(1,15),生成1到15之间的数字。用16位进制函数把生成数字字母化。d