网络编程
位置:首页>> 网络编程>> Asp编程>> 8个asp生成随机字符的函数

8个asp生成随机字符的函数

 来源:asp之家 发布时间:2007-08-04 10:17:00 

标签:随机函数,随机,随机字符,函数

呵呵,先说明一下下面的程序大部分收集自网络,因为本人在asp编程中经常使用到随机函数,所以收集了一些这类的函数,并做了些注释,方便使用。首发在asp之家。别看小小的随机字符串函数其实作用是很大的,就看你怎么发挥了。比如我们可以用他来在生成静态页面时的文件命名,应该很多的网站的都使用过。还有我们可以用它来生成随机密码等等。


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 "<center>"
response.write gen_key(13) ‘这里可以更改长度
response.write "</center>"

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com