手机
当前位置:查字典教程网 >编程开发 >ASP教程 >ASP中一个用VBScript写的随机数类
ASP中一个用VBScript写的随机数类
摘要:外国人写的一个class,这么一点小小的应用,除非有特殊需求,还没有必要模块化。用asp产生一个随机数。

外国人写的一个class,这么一点小小的应用,除非有特殊需求,还没有必要模块化。

用asp产生一个随机数。

<%

''**************************************************************************

''CLASS:cRandom

''Callsrandomizetoseedtherandomnumbergenerator.

''Providesfunctionsforreturningrangedrandomintegersorarraysof

''rangedrandomintegers.

''Callingrandomizetoseedtherandomnumbergeneratoratthetimethe

''classiscreatedseemedlikeareasonablethingtodo.

privatesubClass_Initialize()

''ChecktheVBScriptdocumentationforthespecificsrelating

''totheRandomizefunction

Randomize

endsub

''Terminatedoesn''tneedtodoanythingforthisclass

privatesubClass_Terminate()

endsub

''**********************************************************************

''FUNCTION:RangedRandom

''PARAMETER:lowerBound,thelowestallowablenumbertoreturn

''PARAMETER:upperBound,thehighestallowablenumbertoreturn

''RETURNS:ArandomintegerbetweenlowerBoundandUpperBound,

''inclusive

''**********************************************************************

publicfunctionRangedRandom(lowerBound,upperBound)

RangedRandom=CInt((upperBound-lowerBound)*Rnd+lowerBound)

endfunction

''**********************************************************************

''FUNCTION:RangedRandomArray

''PARAMETER:lowerBound,thelowestallowablenumbertoreturn

''PARAMETER:upperBound,thehighestallowablenumbertoreturn

''PARAMETER:arraySize,zerobasednumberspecifyingthesizeofthearray

''PARAMETER:duplicates,trueorfalsetoindicatewhetherduplicate

''resizethetempArraytoholdthenumberofelementspassedinthe

''arraySizeparameter

redimtempArray(arraySize)

''Thisisaloopcounter,setitto0

filledElements=0

''loopuntilfilledElementsisequaltothearraySize+1

dountilfilledElements=arraySize+1

''CalltheRangedRandomfunctionwiththelowerBoundandupperBoundparameters

tempValue=RangedRandom(lowerBound,upperBound)

''Handlethecasewherewedon''twantduplicatevalues

ifduplicates=falsethen

badValue=false

fori=0toUBound(tempArray)

''checkifthenewrandomvaluealreadyexistsinthearray

''ifitdoessetthebadValueflagtotrueandbreakoutoftheloop

iftempValue=tempArray(i)then

badValue=true

exitfor

endif

next

ifbadValue=falsethen

tempArray(filledElements)=tempValue

filledElements=filledElements+1

endif

else

''Handlethecasewhereduplicatevaluesinthearrayareacceptable

tempArray(filledElements)=tempValue

filledElements=filledElements+1

endif

loop

''returnthearray

RangedRandomArray=tempArray

endfunction

endclass

%>

<%

''Allthecodethatfollowsisexamplecodeshowingtheuseofthe

''cRandomclass.

dimobjRandom

dimflip

dimrandomArray

dimrowsToTest

dimi,j

''createaninstanceofourclass

setobjRandom=newcRandom

''setthenumberofiterationsthatwewanttotest

rowsToTest=10

''"toggle"todeterminewhetherornotwesetthebgcolorofthetablerow

flip=true

''Startthetable

Response.Write"<tableborder=0cellpadding=1cellspacing=1>"

forj=0torowsToTest

''We''llalternatethebgcolorofthetablerowsbasedonthe

''valueoftheflipvariable

ifflipthen

Response.Write"<trbgcolor=LightGrey>"

else

Response.Write"<tr>"

endif

''CalltheRangedRandomArrayfunctionfortestingpurposes

randomArray=objRandom.RangedRandomArray(1,10)

【ASP中一个用VBScript写的随机数类】相关文章:

ASP通用模板类

ASP常用函数:getIMG()

asp 中常用的文件处理函数

ASP 中Split 函数的实例

浅谈ASP中的类

asp中的Rnd 函数

怎样判断一个盘上是否有文件

ASP整理一些实用的速查代码

ASP中数据库调用中常见错误的现象和解决

ASP中的时间函数大全 时间操作函数第1/2页

精品推荐
分类导航