ASP中一个用VBScript写的随机数类(2)
'' 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>"
内容版权声明:除非注明,否则皆为本站原创文章。