当前位置: 代码迷 >> ASP >> 这个是生成数字的随机数,如果要有字母和数字同时显示的请教如何更改
  详细解决方案

这个是生成数字的随机数,如果要有字母和数字同时显示的请教如何更改

热度:76   发布时间:2012-11-06 14:07:00.0
这个是生成数字的随机数,如果要有字母和数字同时显示的请问怎么更改
<!-- #include file="Include/conn.asp" -->
<!-- #include file="Include/Chk.asp" -->
<%
function gen(digits)
dim rndnum,num
Randomize
Do while Len(rndnum)<digits
num = Cstr(Chr((57-48)*rnd+48))
rndnum = rndnum & num
Loop
gen = rndnum
End function

Server.ScriptTimeout=99999999
name=Request.Form("name")
num=Request.Form("num")
if not isnumeric(name) or not isnumeric(num) then
Response.Write("<script>alert('输入错误,产品编码和生成数量都应该是数字!');history.back();</script>")
Response.End()
end if
num=cint(num)
if num>9999 then
Response.Write("<script>alert('每次只能生成9999个防伪码!');history.back();</script>")
Response.End()
end if

Response.ContentType ="application/vnd.ms-Excel"

%>
<table width="300" border="1" cellspacing="0" cellpadding="0">
<%
n=1
do while n<=num
p=name&"-"&gen(4)&"-"&gen(4)&"-"&gen(4)
Set rs=conn.execute("select top 1 id from zm_p where p='"&p&"'")
if rs.eof then
conn.execute("insert into zm_p(p) values('"&p&"')")
n=n+1
%>
  <tr>
  <td align="left">
<%=p%>
</td>
  </tr>
<%
end if
loop
%>
  <tr>
  <td align="right">制表日期:<%=now()%></td>
  </tr>
</table>

------解决方案--------------------
VBScript code

写个函数

<%
'********************************************
'功能:获取指定长度的随机数和字母
'参数:
'    length:要获取的随机字符串长度
'    mode: 1 数字
'           2 小写字母
'           3 大写字母
'           4 数字和小写字母
'           5 数字和大写字母
'           6 小写字母和大写字母
'           7 数字、小写字母和大写字母
'********************************************
Function Gen(length, mode)    
    Dim rndString, rndChar, i, Arr()
    
    Randomize
    Select Case mode
        Case 1  '数字
            For i = 1 To length
                rndChar = Cstr(Chr((57 - 48) * rnd + 48))
                rndString = rndString & rndChar
            Next
        Case 2  '小写字母
            For i = 1 To length
                rndChar = Cstr(Chr((122 - 97) * rnd + 97))
                rndString = rndString & rndChar
            Next
        Case 3  '大写字母
            For i = 1 To length
                rndChar = Cstr(Chr((90 - 65) * rnd + 65))
                rndString = rndString & rndChar
            Next
        Case 4  '数字和小写字母
            ReDim Arr(35)
            For i = 0 To 9
                Arr(i) = CStr(i)
            Next
            For i = 10 To 35
                Arr(i) = CStr(Chr(i + 87))
            Next
            For i = 1 To length
                index = Int((35 - 0) * rnd + 0)
                rndChar = Arr(index)
                rndString = rndString & rndChar
            Next    
        Case 5  '数字和大写字母
            ReDim Arr(35)
            For i = 0 To 9
                Arr(i) = CStr(i)
            Next
            For i = 10 To 35
                Arr(i) = CStr(Chr(i + 55))
            Next
            For i = 1 To length
                index = Int((35 - 0) * rnd + 0)
                rndChar = Arr(index)
                rndString = rndString & rndChar
            Next    
        Case 6  '小写字母和大写字母
            ReDim Arr(51)
            For i = 0 To 25
                Arr(i) = CStr(Chr(i + 97))
            Next
            For i = 26 To 51
                Arr(i) = CStr(Chr(i + 39))
            Next
            For i = 1 To length
                index = Int((51 - 0) * rnd + 0)
                rndChar = Arr(index)
                rndString = rndString & rndChar
            Next  
        Case 7  '数字、小写字母和大写字母
            ReDim Arr(61)
            For i = 0 To 9
                Arr(i) = CStr(i)
            Next
            For i = 10 To 35
                Arr(i) = CStr(Chr(i + 87))
            Next
            For i = 36 To 61
                Arr(i) = CStr(Chr(i + 29))
            Next            
            For i = 1 To length
                index = Int((61 - 0) * rnd + 0)
                rndChar = Arr(index)
                rndString = rndString & rndChar
            Next                          
        Case Else
            rndString = ""
    End Select
    
    Gen = rndString
End Function
%> 
  相关解决方案