四时宝库

程序员的知识宝库

ASP开发者你应该知道(1)字符串生成

最近会陆续贴出ASP开发中常用的Function

ASP常用Function

'==================================================

'函数名:keyGen

'作 用:随机生成字符串,大小写字母及数字

'参 数:digits ------生成字符串的位数

'用 法:keyGen(10)

'==================================================

Function keyGen(digits)

Dim char_array(80) '定义并初始化数组

Dim i, num, output

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((62 - 0 + 1) * Rnd + 0))

output = output + num

Loop

keyGen = output '设置返回值

End Function

'==================================================

'函数名:keyGen

'作 用:随机生成字符串,大小写字母及数字

'参 数:intLength ------生成字符串的位数

'用 法:keyGen(10)

'说 明:该函数中去掉了英文字母O,是因为英文O常与数字0混淆。如果需要可以自行添加。

'==================================================

'另一种方式生产随机字符串包含大小写字母及数字

Function keyGen(intLength)

Dim strSeed, seedLength, pos, Str, i

strSeed = "abcdefghjkmnpqrstuvwxyz"

strSeed = strSeed & UCase(strSeed)

strSeed = strSeed & "1234567890"

seedLength = Len(strSeed)

Str = ""

Randomize

For i = 1 To intLength

Str = Str + Mid(strSeed, Int(seedLength * Rnd) + 1, 1)

Next

keyGen = Str

End Function

'==================================================

'函数名:rndarray

'作 用:生成的是一个不重复的数组

'参 数:istart ------开始数字

'参 数:iend ------结束数字

'参 数:sum ------生成个数

'用 法:rndarray(1,5,1)

'==================================================

Function rndarray(istart,iend,sum)

Dim arrayid(),i,j,blnre,temp,iloop,eloop

Redim arrayid(sum-1)

i=0

iloop=0

eloop=0

blnre=False

Randomize

Do While i<sum

temp=int(rnd*(iend-istart+1)+istart)

If i=0 Then

arrayid(0)=temp

i=i+1

iloop=iloop+1

Else

For j=0 To i-1

if arrayid(j)=temp Then

blnre=True

iloop=iloop+1

Exit For'这一句很重要,防止多余的循环

Else

iloop=iloop+1

End If

Next

If blnre=False Then

arrayid(i)=temp

i=i+1

Else

blnre=False

End If

End If

Loop

rndarray=join(arrayid)

End Function

'==================================================

'函数名:getRndNumber

'作 用:生成一个两个数之间的随机数

'参 数:lowerbound ------开始数字

'参 数:upperbound ------结束数字

'用 法:getRndNumber(100,500)

'==================================================

Function getRndNumber(lowerbound,upperbound)

Randomize

getRndNumber=Int((upperbound-lowerbound+1)*Rnd+lowerbound)

End Function

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接