<% '******************************************************* '* ASP 101 Sample Code - http://www.asp101.com/ * '* * '* This code is made available as a service to our * '* visitors and is provided strictly for the * '* purpose of illustration. * '* * '* http://www.asp101.com/samples/license.asp * '* * '* Please direct all inquiries to webmaster@asp101.com * '******************************************************* %> <% ' Constant representing the number of images. ' This is used in Methods 1 and 2. Const NUMBER_OF_IMAGES = 4 ' Initialize the random number generator. This is the ' command that tells the computer to give us psedo-random ' numbers when we use Rnd later. Randomize ' Note: ' The generic formula for generating a random number in a ' range is: ' ' Int((upperbound - lowerbound + 1) * Rnd + lowerbound) ' ' I've simplified this where possible, but all the random ' generation below is based upon this basic form. '========================================================== ' Method #1: This is probably the easiest and most ' straight-forward approach. We name our image files so ' that they're all the same except for one part of the ' name. This part is then assigned a set of sequential ' numbers. ' ' IE: ad1.gif ' ad2.gif ' ad3.gif ' ad4.gif ' ' This way by simply generating a random number in the ' appropriate range we can easily associate it with a ' particular image. '========================================================== Dim intImageNumber ' A var to store our randomn number ' Pick a random number between 1 and NUMBER_OF_IMAGES (4) intImageNumber = Int((NUMBER_OF_IMAGES * Rnd) + 1) ' Show the corresponding image: %>

Method #1:

Random Image
<% '========================================================== ' Method #2: This method is similar to the first except ' that the options to choose from are hard coded into the ' page explicitly. This allows a few things. First, the ' image names can be whatever you'd like them to be. ' Second, it's easy to associate different parameters with ' the different images as illustrated with the width, ' height, and alt properties below. '========================================================== Dim intImageIdToShow ' Random Id we generate Dim arrImages(3, 3) ' Array to hold image parameters ' Set up our 2D array with the values ' In my setup the first number is basically representing ' an image id and each image has an associated filename, ' width, height, and alt tag text. arrImages(0, 0) = "ad1.gif" arrImages(0, 1) = 150 arrImages(0, 2) = 75 arrImages(0, 3) = "Random Image #1" arrImages(1, 0) = "ad2.gif" arrImages(1, 1) = 150 arrImages(1, 2) = 75 arrImages(1, 3) = "Random Image #2" arrImages(2, 0) = "ad3.gif" arrImages(2, 1) = 150 arrImages(2, 2) = 75 arrImages(2, 3) = "Random Image #3" arrImages(3, 0) = "ad4.gif" arrImages(3, 1) = 150 arrImages(3, 2) = 75 arrImages(3, 3) = "Random Image #4" ' Generate a random number based on the bounds of the ' array we just set up. Notice the ", 1" in the LBound ' and UBound calls. This indicates I want to use the ' bounds from the first dimension. It's the default so I ' could have left it off, but I thought I should include ' it for reference in case you decide to use the array ' differently then I did and need the bound from a ' different dimension. intImageIdToShow = Int((UBound(arrImages, 1) - _ LBound(arrImages, 1) + 1) * Rnd + LBound(arrImages, 1)) ' Show the image: %>

Method #2:

" width = "<%= arrImages(intImageIdToShow, 1) %>" height = "<%= arrImages(intImageIdToShow, 2) %>" alt = "<%= arrImages(intImageIdToShow, 3) %>" />
<% '========================================================== ' Method #3: This method is the most work for the server, ' but it allows you to simply drop a new image into the ' image directory specified and it will automatically ' start showing it randomly with the others. '========================================================== ' Set our random images pickup directory. All files in ' this direcotory will be randomly displayed so it's ' important that the directory contain only image files ' that you want to display. Non-image files and files ' that shouldn't be displayed should not be placed into ' this directory. Const IMGS_DIR = "./rndimgs/" ' Variables for our FileSystemObject objects Dim objFSO, objFolderObject, objFileCollection, objFile ' A pair of integers for our random image selection Dim intFileNumberToUse, intFileLooper ' A "handle" to the file we choose to use Dim objImageFileToUse ' A variable to build our image tag Dim strImageSrcText ' Lets see what's in the directory: Set objFSO = Server.CreateObject("Scripting.FileSystemObject") Set objFolderObject = objFSO.GetFolder(Server.MapPath(IMGS_DIR)) Set objFSO = Nothing Set objFileCollection = objFolderObject.Files Set objFolderObject = Nothing ' Get a count of files and use it to generate a random ' number from 1 to the count. intFileNumberToUse = Int(objFileCollection.Count * Rnd) + 1 ' Set up loop control so we exit when we get to the random ' file number we just picked. intFileLooper = 1 For Each objFile in objFileCollection If intFileLooper = intFileNumberToUse Then ' Get a "handle" on the appropriate file Set objImageFileToUse = objFile Exit For End If intFileLooper = intFileLooper + 1 Next Set objFileCollection = Nothing ' Build our img src tag text strImageSrcText = IMGS_DIR & objImageFileToUse.Name Set objImageFileToUse = Nothing ' Show the image: %>

Method #3:

Random Image
<% '========================================================== ' Interesting Footnote: ' I always used to do things in this order because it ' seemed to make sense to me: ' ' Get FSO ' Get Folder Using FSO ' Get File Collection Using Folder ' Get File Using File Collection ' Do File Stuff ' Release File ' Release File Collection ' Release Folder ' Release FSO ' ' Notice in the code above I do this however: ' ' Get FSO ' Get Folder Using FSO ' Release FSO ' Get File Collection Using Folder ' Release Folder ' Get File Using File Collection ' Release File Collection ' Do File Stuff ' Release File ' ' Notice the difference... in the first method I have 4 ' objects allocated at one point while in the second ' method I never have more then 2 objects open at once! ' ' It seems trivial, but the easiest way to increase ASP ' performance is to get in and out as fast as possible ' and this type of code helps accomplish that. '========================================================== %>