asp生成随机字符的函数

上传人:xiao****1972 文档编号:84143403 上传时间:2019-03-02 格式:DOCX 页数:6 大小:35.20KB
返回 下载 相关 举报
asp生成随机字符的函数_第1页
第1页 / 共6页
asp生成随机字符的函数_第2页
第2页 / 共6页
asp生成随机字符的函数_第3页
第3页 / 共6页
asp生成随机字符的函数_第4页
第4页 / 共6页
asp生成随机字符的函数_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《asp生成随机字符的函数》由会员分享,可在线阅读,更多相关《asp生成随机字符的函数(6页珍藏版)》请在金锄头文库上搜索。

1、8个asp生成随机字符的函数2009-07-22 20:47呵呵,先说明一下下面的程序大部分收集自网络,因为本人在asp编程中经常使用到随机函数,所以收集了一些这类的函数,并做了些注释,方便使用。首发在中国asp之家。别看小小的随机字符串函数其实作用是很大的,就看你怎么发挥了。比如我们可以用他来在生成静态页面时的文件命名,应该很多的网站的都使用过。还有我们可以用它来生成随机密码等等。function MyRandc(n) 生成随机字符,n为字符的个数 ,该随机函数由大小写字母组成,不含数字dim thechr thechr = for i=1 to n dim zNum,zNum2 Rando

2、mize 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(dig

3、its) 定义并初始化数组 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 =

4、 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)=maiwang

5、zhan 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 =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

6、_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 = 6

7、5 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

8、While Len(RndNum) 3,false-003Function 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)

9、 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

10、 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

11、&,) 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)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号