注册 登录
编程论坛 ASP技术论坛

asp怎样生成随机数存入数据库

dhdhzzw 发布于 2007-12-12 09:15, 1330 次点击
想让asp生成随机码存入数据库,但没做过,不知怎么办?
7 回复
#2
madpbpl2007-12-12 10:17
Randomize
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
upperbound 是此范围的上界,而 lowerbound 是此范围内的下界。
#3
dhdhzzw2007-12-12 10:46
我有这段代码,但我如果想产生20个或30个,或自己输入想产生的随机数组数,就产生多少组,我写循环语句却没反应,帮我看看。我贴的是可产生随机数的代码

<%
Function gen_key(digits)

'Create and define array
dim char_array(50)
char_array(0) = "0"
char_array(1) = "1"
char_array(2) = "2"
char_array(3) = "3"
char_array(4) = "4"
char_array(5) = "5"
char_array(6) = "6"
char_array(7) = "7"
char_array(8) = "8"
char_array(9) = "9"
char_array(10) = "A"
char_array(11) = "B"
char_array(12) = "C"
char_array(13) = "D"
char_array(14) = "E"
char_array(15) = "F"
char_array(16) = "G"
char_array(17) = "H"
char_array(18) = "I"
char_array(19) = "J"
char_array(20) = "K"
char_array(21) = "L"
char_array(22) = "M"
char_array(23) = "N"
char_array(24) = "O"
char_array(25) = "P"
char_array(26) = "Q"
char_array(27) = "R"
char_array(28) = "S"
char_array(29) = "T"
char_array(30) = "U"
char_array(31) = "V"
char_array(32) = "W"
char_array(33) = "X"
char_array(34) = "Y"
char_array(35) = "Z"
'Initiate randomize method for default seeding
randomize

'Loop through and create the output based on the the variable passed to
'the function for the length of the key.
do while len(output) < digits
num = char_array(Int((35 - 0 + 1) * Rnd + 0))
output = output + num
loop
'Set return
gen_key = output
End Function

'Write the results to the browser, currently setting a 13 digit key
response.write "<pre>" & gen_key(14) & "</pre>" & vbcrlf
response.write "<pre>" & gen_key(14) & "</pre>" & vbcrlf
response.write "<pre>" & gen_key(14) & "</pre>" & vbcrlf
response.write "<pre>" & gen_key(14) & "</pre>" & vbcrlf
%>
怎样想产生几组就产生几组啊?
#4
madpbpl2007-12-12 15:41
这个例子不是很清楚吗?
function gen_key(digits)
里的digits表示循环的次数,想产生几组只要加个循环就可以了。
比如说
for i=1 to n    'n表示你想产生的组数
response.write "<pre>"&gen_key(digits)&"</pre>"&vbcrlf    'digits根据每组数的位数进行调整
next
#5
dhdhzzw2007-12-12 16:46
楼上的说得对,但我想输入次数就显示多少组,怎么做?
#6
madpbpl2007-12-12 17:01
你输入的次数不就是n吗?试试这样
n=request("n")    'n通过一个表单传过来
if n="" then
n=1
end if
for i=1 to n
.......
#7
dhdhzzw2007-12-21 10:53
不行啊,我加了,代码如下
n=request("n")    'n通过一个表单传过来
if n="" then
n=1
end if
for i=1 to n
do while i = n
next
response.write "<pre>" & gen_key(14) & "</pre>" & vbcrlf
显示缺少语句End function,不知怎么办?〉
#8
dhdhzzw2007-12-21 12:24
做出来了,谢了madpbpl思路正确
1