A random number class written in VBScript in ASP

Source: Internet
Author: User

A class written by a foreigner, such a small application, unless there are special needs, there is no need for modularization.
Generate a random number using ASP.
<%
''************************************** ************************************
''Class: crandom
''Calls randomize to seed the random number generator.
''Provides functions for returning ranged random integers or arrays
''Ranged random integers.
''Calling randomize to seed the random number generator at the time
''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,
''Comprehensive
''************************************** ********************************
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
''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
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
''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 alterate the bgcolor of the table rows based on
''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)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.