ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
What is Adovbs.inc and Why Do I Need It?
An Overview of ASP.NET
Connections, Commands, And Procedures

QUICK TIP:
Batch your code to save processing time
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


<%
'************************************************************************** ' 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
%>
Processing Time: 0 seconds
Lines Processed: 148

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers