Printer Friendly Version

BellaOnline's ASP Editor

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


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!

Introduction to ASP Ebook

Download this ebook to get everything you need to know about learning ASP - from a step by step tutorial to function lists, sample code, common errors and solutions, and much more! 101 pages.

ASP Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 by Lisa Shea. All rights reserved.
This content was written by Lisa Shea. If you wish to use this content in any manner, you need written permission. Contact Lisa Shea for details.

| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2016 Minerva WebWorks LLC. All rights reserved.

BellaOnline Editor