当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP中一个用VBScript写的随机数类

ASP
ASP编程入门进阶(十二):ASP技巧累加(一)
ASP编程入门进阶(十三):Ad & Content Rotator
ASP编程入门进阶(十四):Browser & Linking
ASP编程入门进阶(十五):组件Counters
一些简单的ASP练习
ASP编程入门进阶(十六):FSO组件之驱动器操作
ASP编程入门进阶(十七):FSO组件之文件夹操作
ASP编程入门进阶(十八):FSO组件之文件操作(上)
ASP编程入门进阶(十八):FSO组件之文件操作(中)
ASP编程入门进阶(十八):FSO组件之文件操作(下)
ASP编程入门进阶(十九):ASP技巧累加(二)
ASP中有关双引号,单引号以及&号的解释
ASP编程入门进阶(二十):ADO组件之显示数据记录
ASP编程入门进阶(二十):ADO组件之插入数据记录
ASP编程入门进阶(二十):ADO组件之分页程序
ASP编程入门进阶(二十):ADO组件之修改数据记录
ASP编程入门进阶(二十):ADO组件之删除数据记录
ASP初学者常犯的几个错误(ZT)
无组件上传图片之文件采用方案
ASP编程入门进阶(二十):ADO组件之查询数据记录

ASP中一个用VBScript写的随机数类


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 100 ::
收藏到网摘: n/a

外国人写的一个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)