技术库 技术手册 字体库 知识点 词汇表 联系我们
内容搜索   
本站最新推出网站制作字体库。
当前位置:WEB技术知识资源库(www.cn-web.com) .: Asp技术 .: Asp代码库 .: asp中生成随机字符的函数

asp中生成随机字符的函数


1.生成随机字符,n为字符的个数 ,该随机函数由大小写字母组成,不含数字
function MyRandc(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
使用方法:
response.write MyRandn(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 "<center>"
response.write gen_key(13) ‘这里可以更改长度
response.write "</center>"

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.生成任意位随机数的函数
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.生成随机字符串,包括大小写字母,数字,和其它符合,常用于干扰码。
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)








对此文章打分评级

用户评论

增加评论
此文章还没有任何评论!
网站地图 - 知识词汇 - 全文检索 - 广告服务 - 帮助中心 - 联系我们
.:www.cn-web.com
网站技术开发联盟之WEB开发技术知识库
联系人:老韩(QQ:5679551)
晋ICP备07003487号