新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > Web开发 > VBScript教程 > 正文:ASP中一个用VBScript写的随机数类

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

新客网 XKER.COM 2004-04-28 来源: 收藏本文
<%
'**************************************************************************
' CLASS: cRandom
' Calls randomize to seed the random number generator.
' Provides functions for returning ranged random integers or arrays of
' ranged random integers.
'**************************************************************************
class cRandom

' 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
' random values are allowed in the array
' RETURNS: A single dimension array sized to match the arraySize
' parameter, containing random integers between lowerBound and
' upperBound, inclusive
'**********************************************************************
public function RangedRandomArray(lowerBound, upperBound, arraySize, duplicates)
dim tempArray()
dim filledElements, tempValue, badValue, i

' 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,
标签:
收藏】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
【重要声明】:新客网刊载此文仅为提供更多信息的目的,并不代表新客网同意文章的说法或描述,也不构成任何建议,对本文有任何异议,请在上面提出建议。
  • 阅读排行
  • 推荐阅读
  • 随机推荐