ASP中一个用VBScript写的随机数类
程序员文章站
2022-04-14 18:57:08
外国人写的一个class,这么一点小小的应用,除非有特殊需求,还没有必要模块化。 用asp产生一个随机数。 <% ''******************...
外国人写的一个class,这么一点小小的应用,除非有特殊需求,还没有必要模块化。
用asp产生一个随机数。
<%
''**************************************************************************
'' class: crandom
'' calls randomize to seed the random number generator.
'' provides functions for returning ranged random integers or arrays of
'' ranged random integers.
'' calling randomize to seed the random number generator at the time the
'' class is created seemed like a reasonable thing to do.
private sub class_initialize()
'' check the vbscript documentation for the specifics relating
'' to the randomize function
randomize
end sub
'' terminate doesn''t need to do anything for this class
private sub class_terminate()
end sub
''**********************************************************************
'' function: rangedrandom
'' parameter: lowerbound, the lowest allowable number to return
'' parameter: upperbound, the highest allowable number to return
'' returns: a random integer between lowerbound and upperbound,
'' inclusive
''**********************************************************************
public function rangedrandom( lowerbound, upperbound )
rangedrandom = cint((upperbound - lowerbound) * rnd + lowerbound)
end function
''**********************************************************************
'' function: rangedrandomarray
'' parameter: lowerbound, the lowest allowable number to return
'' parameter: upperbound, the highest allowable number to return
'' parameter: arraysize, zero based number specifying the size of the array
'' parameter: duplicates, true or false to indicate whether duplicate
'' resize the temparray to hold the number of elements passed in the
'' arraysize parameter
redim temparray(arraysize)
'' this is a loop counter, set it to 0
filledelements = 0
'' loop until filledelements is equal to the arraysize + 1
do until filledelements = arraysize + 1
'' call the rangedrandom function with the lowerbound and upperboundparameters
tempvalue = rangedrandom( lowerbound, upperbound )
'' handle the case where we don''t want duplicate values
if duplicates = false then
badvalue = false
for i = 0 to ubound(temparray)
'' check if the new random value already exists in the array
'' if it does set the badvalue flag to true and break out of the loop
if tempvalue = temparray(i) then
badvalue = true
exit for
end if
next
if badvalue = false then
temparray(filledelements) = tempvalue
filledelements = filledelements + 1
end if
else
'' handle the case where duplicate values in the array are acceptable
temparray(filledelements) = tempvalue
filledelements = filledelements + 1
end if
loop
'' return the array
rangedrandomarray = temparray
end function
end class
%>
<%
'' all the code that follows is example code showing the use of the
'' crandom class.
dim objrandom
dim flip
dim randomarray
dim rowstotest
dim i, j
'' create an instance of our class
set objrandom = new crandom
'' set the number of iterations that we want to test
rowstotest = 10
'' "toggle" to determine whether or not we set the bgcolor of the table row
flip = true
'' start the table
response.write "<table border=0 cellpadding=1 cellspacing=1>"
for j = 0 to rowstotest
'' we''ll alternate the bgcolor of the table rows based on the
'' value of the flip variable
if flip then
response.write "<tr bgcolor=lightgrey>"
else
response.write "<tr>"
end if
'' call the rangedrandomarray function for testing purposes
randomarray = objrandom.rangedrandomarray( 1, 10)
用asp产生一个随机数。
<%
''**************************************************************************
'' class: crandom
'' calls randomize to seed the random number generator.
'' provides functions for returning ranged random integers or arrays of
'' ranged random integers.
'' calling randomize to seed the random number generator at the time the
'' class is created seemed like a reasonable thing to do.
private sub class_initialize()
'' check the vbscript documentation for the specifics relating
'' to the randomize function
randomize
end sub
'' terminate doesn''t need to do anything for this class
private sub class_terminate()
end sub
''**********************************************************************
'' function: rangedrandom
'' parameter: lowerbound, the lowest allowable number to return
'' parameter: upperbound, the highest allowable number to return
'' returns: a random integer between lowerbound and upperbound,
'' inclusive
''**********************************************************************
public function rangedrandom( lowerbound, upperbound )
rangedrandom = cint((upperbound - lowerbound) * rnd + lowerbound)
end function
''**********************************************************************
'' function: rangedrandomarray
'' parameter: lowerbound, the lowest allowable number to return
'' parameter: upperbound, the highest allowable number to return
'' parameter: arraysize, zero based number specifying the size of the array
'' parameter: duplicates, true or false to indicate whether duplicate
'' resize the temparray to hold the number of elements passed in the
'' arraysize parameter
redim temparray(arraysize)
'' this is a loop counter, set it to 0
filledelements = 0
'' loop until filledelements is equal to the arraysize + 1
do until filledelements = arraysize + 1
'' call the rangedrandom function with the lowerbound and upperboundparameters
tempvalue = rangedrandom( lowerbound, upperbound )
'' handle the case where we don''t want duplicate values
if duplicates = false then
badvalue = false
for i = 0 to ubound(temparray)
'' check if the new random value already exists in the array
'' if it does set the badvalue flag to true and break out of the loop
if tempvalue = temparray(i) then
badvalue = true
exit for
end if
next
if badvalue = false then
temparray(filledelements) = tempvalue
filledelements = filledelements + 1
end if
else
'' handle the case where duplicate values in the array are acceptable
temparray(filledelements) = tempvalue
filledelements = filledelements + 1
end if
loop
'' return the array
rangedrandomarray = temparray
end function
end class
%>
<%
'' all the code that follows is example code showing the use of the
'' crandom class.
dim objrandom
dim flip
dim randomarray
dim rowstotest
dim i, j
'' create an instance of our class
set objrandom = new crandom
'' set the number of iterations that we want to test
rowstotest = 10
'' "toggle" to determine whether or not we set the bgcolor of the table row
flip = true
'' start the table
response.write "<table border=0 cellpadding=1 cellspacing=1>"
for j = 0 to rowstotest
'' we''ll alternate the bgcolor of the table rows based on the
'' value of the flip variable
if flip then
response.write "<tr bgcolor=lightgrey>"
else
response.write "<tr>"
end if
'' call the rangedrandomarray function for testing purposes
randomarray = objrandom.rangedrandomarray( 1, 10)