网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/10/8 浏览:3 次 )
外国人写的一个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)
上一篇:文件、目录,文本文件等多种操作类
下一篇:浅谈ASP中的类
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?