Rnd Function
Tips >>ASP Functions

WANTS TO MAKE SOFTWARE WITHOUT KNOWLEDGE OF ASP? CLICK HERE

The Rnd function returns a random number. The number is always less than 1 but greater or equal to 0. However, the Rnd Function returns a random number less than one. So, in order to get a whole number, you need to use the Int function. If you need a larger range than 0 or 1, you need to multiply the result. Rnd is any valid numeric expression. For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence. The Rnd function generates a pseudo-random number greater than or equal to 0.0 and less than 1.0. The Number argument is optional, but different numbers will give different pseudo-random numbers.

To produce random integers in a given range, use this formula :-

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

The Syntax of the Rnd function is :-

Rnd[(number)]

The value of number determines how Rnd generates a random number:-

If number is Rnd generates
Less than zero The same number every time, using number as the seed.
Greater than zero The next random number in the sequence.
Equal to zero The most recently generated number.
Not supplied THe next random number in the sequence.

Example#1 :-

Code :-
<%
document.write(Rnd)
%>
Output :-
0.7055475

Example#2 :-

Code :-
'If you refresh the page,
'using the code in example 1,
'the SAME random number will show over and over.
'Use the Randomize statement generate a new random number'
each time the page is reloaded!
Randomize
document.write(Rnd)
Output :-
0.4758112

Example#3 :-

Code :-
'Here is how to produce random integers in a
'given range:
dim max,min
max=100
min=1
document.write(Int((max-min+1)*Rnd+min))
Output :-
71


It can be better explained in the following ways :-

Example#4 :-

Code :-
<%
=Rnd()
%>
Output :-
0.7055475

Example#5 :-

Code :-
<%
=Rnd(127.89)
%>
Output :-
0.533424

Example#6 :-

Code :-
<%
=Rnd(-127.89)
%>
Output :-
0.6953956

Example#7 :-

Code :-
<%
Randomize %>
<% =Rnd()
%>
Output :-
0.1414095

Example#8 :-
The following code will generate a random number between any limits you choose. (Since the Int function always rounds down, we add one to the difference between the limits.)

Code :-
<% upperlimit = 50000.0 %>
<% lowerlimit = -30000.0 %>
<% =Int((upperlimit - lowerlimit + 1)*Rnd() + lowerlimit) %>
Output :-
30366

Note :- To repeat sequences of random numbers, call the Rnd function with a negative argument immediately before using the Randomize statement with a numeric arguments. Using the Randomize statement with the same value for number does not repeat the previous sequence.

 
E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641