中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > Web开发 > Asp > 综合文章
ASP中一个用VBScript写的随机数类
作者:asilas 时间:2002-03-23 11:15 出处:互联网 责编:chinaitpower
              摘要:ASP中一个用VBScript写的随机数类
演示:
627416185832865380
602308334917822171
1699539553392251437
6575829327214661252
94502976285521100712
7772633066757355648
49845344296862635580
2259334956947914666
26793753102161635673
4983608565998581399
365657412091167684
<%
	'**************************************************************************
	' 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)
			Response.Write "<td align=right width=30><font face=Verdana size=1>" & randomArray(i) & "</font></td>"
		next
		Response.Write "</tr>"
		' Toggle the flip boolean variable
		flip = not flip
	next
	
	' Close the table
	Response.Write "</table>"
	
	' Destroy the instance of our object
	set objRandom = nothing
%>
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有