In ASP, a random number class _asp class that is written in VBScript

Source: Internet
Author: User
Tags generator
Foreigners write a class, such a small application, unless there is a special need, there is no need for modularity.
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 of
' Ranged random integers.
"Calling Randomize to Seed" 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
End Sub
' Terminate doesn ' t need to does 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 and set it to 0
filledelements = 0
' Loop until filledelements is equal to the ArraySize + 1
Do Until filledelements = arraysize + 1
"Call" 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 the ' loop
If Tempvalue = Temparray (i) Then
Badvalue = True
Exit For
End If
If Badvalue = False Then
Temparray (filledelements) = Tempvalue
filledelements = filledelements + 1
End If
' Handle the case where duplicate values in the array are acceptable
Temparray (filledelements) = Tempvalue
filledelements = filledelements + 1
End If
"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's our class
Set objrandom = new Crandom
' Set the number of iterations ' we want to test
Rowstotest = 10
' "Toggle" to determine whether or not we set the bgcolor of the table row
Flip = True
' Start ' 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>"
Response.Write "<tr>"
End If
"Call" 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: 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.