ASP中一个用VBScript写的随机数类
2006-08-27 00:00
656 查看
外国人写的一个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产生一个随机数。
<%
''**************************************************************************
'' 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、vbscript编码模板
- 利用vbscript脚本修改文件内容,此适用于自动化的操作中
- asp,VBscript语法错误,史上最全最详细最精确
- vbscript脚本编程教程2利用fso来进行文件操作
- 使用vbscript脚本在表单中进行选择的代码
- 用vbscript脚本实现返回 IP 配置数据的代码
- ASP里面令人震撼地Debug类(VBScript)
- 调试JavaScript/VBScript脚本程序(IE篇)
- JavaScript/VBScript脚本程序调试(Wscript篇)
- 枚举域内计算机个数vbscript脚本(没环境,没测试)
- ASP/VBScript中CHR(0)的由来以及带来的安全问题分析
- ASP(VBScript)中整除和取余
- ASP基础知识VBScript基本元素讲解
- ASP基础入门第四篇(脚本变量、函数、过程和条件语句)
相关文章推荐
- ASP中一个用VBScript写的随机数类
- 【ASP】ASP vbscript一个综合的例子,操作html文件添加、删除代码、删除文件。
- asp vbscript常用函数语语法备查,一个帮助文档,对写asp程序很有帮助的
- ASP中一个字符串处理类(加强)(VBScript)
- ASP中一个字符串处理类(加强)(VBScript)
- ASP中一个字符串处理类(VBScript) 原版
- 一个ASP(VBScript)简单SQL语句构建“类”
- asp中提示至少一个参数没有被指定值 解决方法
- asp.net mvc下标记一个action同时可以接受httpget和httppost
- asp的offset的一个go to page第1/2页
- 一个完整的 ASP.NET 2.0 URL 重写方案
- Asp.net 2.0 自定义控件开发[开发一个图表(WebChart)控件(柱状图示例)](示例代码下载)
- Asp.net 2.0 自定义控件开发[开发一个图表(WebChart)控件(柱状图示例)](示例代码下载)
- ASP.NET 2.0数据教程之二:创建一个业务逻辑层
- Scott Mitchell 的ASP.NET 2.0数据教程之二:创建一个业务逻辑层
- Scott Mitchell 的ASP.NET 2.0数据教程之一: 创建一个数据访问层
- 一个asp.net Page的生死轮回
- 封装一个MongoDB的 asp.net 链接类
- 一个简单的iTextSharp的ASP.NET的例子(根据iText的jsp版本转换而来)
- 用ASP.NET写一个发送ICQ信息的程序