知识大全 ASP中一个用VBScript写的随机数类
Posted 外国人
篇首语:敢说敢作敢为, 无怨无恨无悔。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 ASP中一个用VBScript写的随机数类相关的知识,希望对你有一定的参考价值。
ASP中一个用VBScript写的随机数类 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
外国人写的一个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 functionRandomizeend sub
Terminate doesn t need to do anything for this classprivate 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 beeen 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 parameterredim tempArray(arraySize)
This is a loop counter set it to filledElements =
loop until filledElements is equal to the arraySize + do until filledElements = arraySize +
Call the RangedRandom function with the lowerBound and upperBoundparameterstempValue = RangedRandom( lowerBound upperBound )
Handle the case where we don t want duplicate valuesif duplicates = false thenbadValue = falsefor i = 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 loopif tempValue = tempArray(i) thenbadValue = trueexit forend ifnextif badValue = false thentempArray(filledElements) = tempValuefilledElements = filledElements + end ifelse Handle the case where duplicate values in the array are acceptabletempArray(filledElements) = tempValuefilledElements = filledElements + end ifloop
return the arrayRangedRandomArray = tempArrayend function
end class%><% All the code that follows is example code showing the use of the cRandom class
dim objRandomdim flipdim randomArraydim rowsToTestdim i j
create an instance of our classset objRandom = new cRandom
set the number of iterations that we want to testrowsToTest =
toggle to determine whether or not we set the bgcolor of the table rowflip = true
Start the tableResponse Write <table border= cellpadding= cellspacing= >
for j = to rowsToTest We ll alternate the bgcolor of the table rows based on the value of the flip variableif flip thenResponse Write <tr bgcolor=LightGrey> elseResponse Write <tr> end if
cha138/Article/program/net/201311/13340相关参考