Codefixer - ASP tutorials, resources and software
ASP.NET 3.5/2.0 Hosting – Click Here
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Wednesday 14 May
MembersPro PayPal - ASP Membership software

asp.netPRO “Best ASP.NET Host” – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

Generate a Random Number

The Rnd function returns a value less than 1 but greater than or equal to 0.
Note that to use the Rnd function properly you must place the Randomize statement somewhere before it to ensure that a new random number is generated each time the page is reloaded. Have a look at the following code in Example 1.

Example 1;

<%
Dim MyRandomNum
Randomize
MyRandomNum = Rnd
Response.Write MyRandomNum
%>

The above code will produce a different number each time. Below are typical numbers produced.
0.9923431
0.2924431

Example 2 below shows the code to create a number between 1-100.

Example 2;

<%
Dim MyNewRandomNum
Randomize
MyNewRandomNum = (Rnd * 100)+1
response.write MyNewRandomNum
%>

The code above will produce a number such as 55.9393933
To get rid of the decimals we can use the Int function as in example 3 below.

Example 3;

<%
Dim MyNewRandomNum
Randomize
MyNewRandomNum = Int(Rnd * 100)+1
response.write MyNewRandomNum
%>

The above code chops off the decimals and leaves you with a number such as 55.
Note that it doesn't round the number of, it merely chops off the decimals.
To round of the number use the Round function instead of Int as is demonstrated in example 4 below.

Example 4;

<%
Dim MyNewRandomNum
Randomize
MyNewRandomNum = Round(Rnd * 100)+1
response.write MyNewRandomNum
%>

Related tutorial 'Generate a random number between 2 numbers'
Here's the code to display a random quote

LinksPro - Directory  and Link  Management Software




ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software