加载中...
查看文章
  • Asp生成随机字符的函数
  • 作者:未知 来源:本站整理 添加时间:2009年7月10日 字体:
  • 87043随机字符在asp中用途很多,比如给上传的图片文件命名,登陆验证码,还有文章类网站的干扰码等等,下面列举了8个常用的随机字符的函数.
    {.";
    {.";1.
    {.";function MyRandc(n)
    {.";’生成随机字符,n为字符的个数 ,该随机函数由大小写字母组成,不含数字
    {.";dim thechr
    {.";thechr = ""
    {.";for i=1 to n
    {."; dim zNum,zNum2
    {."; Randomize
    {."; zNum = cint(25*Rnd)
    {."; zNum2 = cint(10*Rnd)
    {."; if zNum2 mod 2 = 0 then
    {."; zNum = zNum + 97
    {."; else
    {."; zNum = zNum + 65
    {."; end if
    {."; thechr = thechr & chr(zNum)
    {.";next
    {.";MyRandc = thechr
    {.";end function
    {.";
    {.";使用方法:
    {.";
    {.";MyRandc(n) 生成随机字符,n为字符的个数,
    {.";
    {.";如:
    {.";response.write MyRandn(10)
    {.";输出10个随机英文字母字符
    {.";
    {.";2.’功能说明:生成指定长度的随机字符,大小写英文字母加数字
    {.";function gen_key(digits)
    {.";’定义并初始化数组
    {.";dim char_array(80)
    {.";’初始化数字
    {.";for i = 0 to 9
    {.";char_array(i) = cstr(i)
    {.";next
    {.";’初始化大写字母
    {.";for i = 10 to 35
    {.";char_array(i) = chr(i + 55)
    {.";next
    {.";’初始化小写字母
    {.";for i = 36 to 61
    {.";char_array(i) = chr(i + 61)
    {.";next
    {.";randomize ’初始化随机数生成器。
    {.";do while len(output) < digits
    {.";num = char_array(int((61 - 0 + 1) * rnd + 0))
    {.";output = output + num
    {.";loop
    {.";’设置返回值
    {.";gen_key = output
    {.";end function使用方法:
    {.";把结果返回给浏览器
    {.";response.write "本实例生成的十三位随机字符串为:"
    {.";response.write "
    "
    {.";response.write gen_key(13) ‘这里可以更改长度
    {.";response.write "
    "
    {.";
    {.";3.这个虽然不是函数,也不是常规的随机函数,但是当我{.";们想让它随机输出我{.";们指定的字符时,可以用到下面的方法。
    {.";
    {.";dim a(5)
    {.";randomize
    {.";t=int(rnd*5)
    {.";a(0)="yingyu"
    {.";a(1)="laoda"
    {.";a(2)="wangzhan"
    {.";a(3)="maiwangzhan"
    {.";a(4)="hehe"
    {.";使用方法:
    {.";response.Write(a(t))
    {.";
    {.";
    {.";4.功能说明:返回16位随机字符,大小写英文字母加数字
    {.";public function Generate_Key()
    {.";
    {.";Randomize
    {.";
    {.";do
    {.";num = Int((75 * Rnd)+48)
    {.";found = false
    {.";if num >= 58 and num <= 64 then
    {.";found = true
    {.";else
    {.";if num >=91 and num <=96 then
    {.";found = true
    {.";end if
    {.";end if
    {.";if found = false then
    {.";RSKey = RSKey+Chr(num)
    {.";end if
    {.";loop until len(RSKey)=16
    {.";
    {.";Generate_Key=RSKey
    {.";
    {.";end function 使用方法:
    {.";Response.Write Generate_Key()
    {.";
    {.";5.功能说明:返回指定长度的随机字符,大小写英文字母加数字组成
    {.";
    {.";function makePassword(byVal maxLen)
    {.";Dim strNewPass
    {.";Dim whatsNext, upper, lower, intCounter
    {.";Randomize
    {.";For intCounter = 1 To maxLen
    {.";whatsNext = Int((1 - 0 + 1) * Rnd + 0)
    {.";If whatsNext = 0 Then
    {.";upper = 90
    {.";lower = 65
    {.";Else
    {.";upper = 57
    {.";lower = 48
    {.";End If
    {.";strNewPass = strNewPass & Chr(Int((upper - lower + 1) * Rnd + lower))
    {.";Next
    {.";makePassword = strNewPass
    {.";end function使用方法:dim radpass
    {.";response.write makePassword(6) ’6位字符
    {.";6.
    {.";功能说明:生成任意位随机数的函数
    {.";'strLong是随机数字的位数,返回指定长度的数字串,不含字母
    {.";Function rndNum (strLong)
    {.";Dim temNum
    {.";Randomize
    {.";Do While Len(RndNum) < strLong
    {.";temNum=CStr(Chr((57-48)*rnd+48))
    {.";RndNum=RndNum&temNum
    {.";loop
    {.";End Function
    {.";使用方法:
    {.";response.write rndNum(6)
    {.";
    {.";
    {.";7.功能说明:取指定范围内的指定个数的随机数,无重复数
    {.";
    {.";指定5个参数:
    {.";iLessCount----取最少n个数(Integer)
    {.";iMostCount----取最多n个数(Integer)
    {.";iLessNumber----取数最小范围(Integer)
    {.";iMostNumber----取数最大范围(Integer)
    {.";cutZero----是否要剔除无效零(Boolean)[例:true-->3,false-->003]
    {.";
    {.";Function rndNumber(iLessCount,iMostCount,iLessNumber,iMostNumber,cutZero)
    {."; If iLessCount = 0 OR iMostCount < iLessCount OR NOT _
    {."; isnumeric(iLessCount) OR NOT isnumeric(iMostCount) OR NOT _
    {."; isnumeric(iLessNumber) OR NOT isnumeric(iMostNumber) _
    {."; OR (iLessNumber = iMostNumber) Then Exit Function
    {."; ’最少个数零、最大个数小于最小个数、4个参数不为数字、最小数等于最大数就退出函数
    {."; Randomize
    {."; Dim iRnd,sZero
    {."; Dim sOutput
    {."; Dim iLength
    {."; Dim sTempOutput
    {."; Dim i
    {."; Dim iCount
    {."; iCount = int(rnd*(iMostCount-iLessCount+1))+iLessCount’计算随机取几个数
    {."; iLength = len(iMostNumber)*iCount+(iCount*2)
    {."; ’长度为最大数长度乘以随机个数加上随机个数乘以2(每个数前后各一个逗号,用来全字匹配)
    {."; Do While len(sOutput) < iLength’输出小于长度时循环
    {."; iRnd = int(rnd*(iMostNumber-iLessNumber+1))+iLessNumber’取随机数
    {."; If Len(iRnd) < len(iMostNumber) Then’随机数长度小于取最大数长度
    {."; For i = 1 To len(iMostNumber) - len(iRnd)’那么就要在首位加缺少的零
    {."; sZero = sZero & "0"
    {."; Next
    {."; End If
    {."; iRnd = sZero & iRnd’把零加在随机数前面
    {."; sZero = empty’清空首位零,循环后还要调用
    {."; If Instr(sOutput,","&iRnd&",") < 1 Then’不在输出变量中就放进去0
    {."; sOutput = sOutput & "," & iRnd & ","
    {."; End If
    {."; Loop
    {.";
    {."; sOutput = mid(sOutput,2,len(sOutput)-2)’去掉首尾逗号
    {."; sOutput = Replace(sOutput,",,",",")’把双逗号替换成单逗号
    {.";
    {."; If cutZero = true Then’如果要去除首位多余的零
    {."; sTempOutput = split(sOutput,",")’拆分为数组
    {."; sOutput = empty’清空,后面要重新放入
    {."; For i = 0 To Ubound(sTempOutput)’逐个转换成数值后放入
    {."; sOutput = sOutput & Clng(sTempOutput(i)) & ","
    {."; Next
    {."; sOutput = mid(sOutput,1,len(sOutput)-1)’去掉末尾逗号
    {."; End If
    {."; rndNumber = sOutput’输出
    {.";End Function
    {.";使用方法:
    {.";Response.Write rndNumber(1,3,5,15,false)
    {.";
    {.";8.功能说明:生成随机字符串,包括大小写字母,数字,和其它符合,常用于干扰码。
    {.";
    {.";参数说明:stars--干扰码最小长度,ends--干扰码最大长度
    {.";
    {.";function rndcode(byVal stars,byVal ends)
    {.";'by 天空诚 from:aspxhome.com
    {.";dim rndlen,i
    {.";randomize
    {.";rndLen = int(stars*rnd+ends-stars)
    {.";for i = 1 to rndLen
    {."; randomize
    {."; rndcode = rndcode & chr(int(127*rnd+1))
    {.";next
    {.";end function
    {.";使用方法:response.write rndcode(20,100)87040
  • 返回页面顶端
  • 上一篇:css教程:DIV布局网页的常见错误
  • 下一篇:爆笑情侣北京方言对骂
  • 返回上一页 打印本文 加入收藏
  • 页面最后更新时间:2018年11月24日