ASP中一個用VBScript寫的隨機數類

來源:互聯網
上載者:User

外國人寫的一個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)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.