ASP Is Fun

Learning ASP
Free Course
Basics
Functions
SQL Commands
Sample Code

Servers / Setup
Host Reviews
Server Setup
Error Tips

Contract ASP
Development

Intro to ASP



 


Random Numbers in ASP



There are many situations in which you'll want to have a random number in your ASP script. This can be used to change ads, to generate greetings, and much more.

First, a computer needs to know where to 'start' in generating its random number. Otherwise it'll generate the same sequence of numbers. It's only a computer after all and can't think - it can just follow instructions. So before you do anything else, you must issue the command

Randomize()

That starts your random number generator off at a spot based on the system timer, so it'll always be different.

Next, any time you reference the RND variable, it'll be set to a random value between 0 and 1. So it could be .3, .6, 0, 1, or any other number in that range.

So your aim is to get that number into the range you want it in, and to then use INT to chop off any decimal points. Let's say you want a number between 1 and 10. You would multiply that starting number by 9, and add 1 so that a 0 turned into a 1, a 1 into a 2 and so on. The lowest number it could create would be 1 (0 + 1), and the highest number would be 10 (9 + 1). Your formula would look like

RandNumb = Int((9 * Rnd) + 1)

You can use random numbers to display different ads, to change greetings, to generate random tips or fortune cookies. Have fun!

Numeric / Math ASP Functions


ASP Main Page | ASP Ebooks | Free ASP Course | Contact Lisa
All content copyright © 2008 Minerva WebWorks LLC. All rights reserved.

Italian Wedding


Free Newsletter!

Ebooks
Intro to ASP
ASP SQL DB
Members
Shopping


Introduction to ASP

Romance Tips
Irish Romance
Italian Romance
French Romance

ASP Poll
How do you handle 500 errors?
I don't
Page does nothing
Displays error
Emails error