Random Numbers and ASP

Have you ever wanted to create a webpage that is dynamic and ever changing? In this article, I will show you how to use VBScript's Randomize and Rnd functions in ASP to generate near random numbers that can be used to create dynamic effects and content.

To begin, let's review the Rnd and Randomize functions, which are at the core of random numbers in ASP. The Rnd function produces a fractional number between zero and one. The way in which it generates the fraction depends on a complex formula and a starting number or seed. Without having a different starting number each time it is called, Rnd returns the same fractional number, which is obviously not the desired effect. So here is where the Randomize function comes into the mix. Randomize makes use of the computer's system clock to provide a different seed to Rnd on each occasion. So combining Rnd with Randomize enables Rnd to effectively produce a random fractional number.

For example, set up an ASP page with the following code, load it, and then refresh your browser several times to see how the number written is different each time the page loads:

By the way, the number produced is not actually "random" as random is meant in strict mathematical parlance, but instead is a simulation of a random number, which for all intents and purposes is good enough.

Having described how Randomize and Rnd works, I'll now briefly show how to use them in an application. As a fractional number, the output from Rnd is not very useful. So, here you'll have to create a function that will convert the fractional number into something that can be used, such as an integer between one and X. To generate a random integer between one and X, multiply the fraction produced by Rnd by an integer X and convert the result into an integer (using VBScript's Int function which simply discards the fractional part of a number) and then add one to the result.

Try the following code, which simulates the rolling of a die, as an example:

Naturally, it is possible to create variations of this function to produce different kinds of near random numbers such as a random number between the integers Y and Z. How your function reads and works depends on what you would like to accomplish.

Once you have a random integer, you basically have a means to create on the Web all kinds of dynamic effects and content, games and puzzles, experiments, and the like. What I will show you in the remainder of this article is two examples of how random numbers can be used in ASP to produce an ever changing page.

The first example is a tip of the day page. Many a desktop application has a random tip of the day to help users become acquainted with it. With random numbers and ASP, a tip of the day can easily be added to your Web application.

For the sake of this example, let's say you have ten tips and you would like to display a tip randomly each time the tips page loads. To set up the "database" of tips, you can use a VBScript array within the ASP page, a text file with each line representing a different tip, or a relational database such as Access or SQL Server with each tip stored as a record in a table. If you were to use a relational database, then you should be careful to ensure that the field in the table of records that identifies each record does not have any missing numbers; otherwise a select query like the following might not yield the desired effect:

For the purpose of this article, I will describe how to set up the random tip generator using an array, but of course the concept is largely the same irrespective of where the data is stored.

The first thing to do is to ensure that your page will not be cached by the client's browser or by a proxy server. The reason for this is that the content of the page should always be fresh if the tip generator is to work properly. Next, set up the database of tips. By using an array to store the tips, this part is just a matter of dimensioning an array and setting each element of the array to hold a different tip. Having done that, establish a random number function to produce a random number between one and ten. Then dimension a string variable, set the variable to equal the ith element of the array, where i is the random number between one and ten. Finally, in the HTML part of the page, print out the contents of this variable. The result is a "random" tip of the day.

In conclusion, random numbers can be used in ASP to produce all kinds of wonderful, dynamic effects. www.database-applications.net is one Web application that uses random numbers and ASP in interesting ways. With random numbers, your Web application is limited only by your imagination.

Author Profile

Stephen Lian is a Web application developer in Australia, specialising in Microsoft solutions. He is the creator of www.database-applications.net, a global IT services marketplace, a knowledge base of Microsoft technologies, and a directory of software and Web sites.