分享
 
 
 

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

王朝asp·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

演示:

62741618583286538060230833491782217116995395533922514376575829327214661252945029762855211007127772633066757355648498453442968626355802259334956947914666267937531021616356734983608565998581399365657412091167684

<%

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

' 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, 100, 9, false )

' Output the contents of the array

for i = 0 to UBound(randomArray)

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有