We help IT Professionals succeed at work.

# ASP random string

on
Medium Priority
902 Views
I have the following ASP functions that builds a string based on randomly generated integer values (and converts them to their corresponding ASCII values).....The functions are working properly, but I need the string to only allow ASCII values in the range of 48 to 122.  Currently the range has no lower limit, only an upper limit.

How can I generate random numbers in the range of 48 to 122 ?  I think it might be possible using modular division.

<%
function generate()

dim randomTemp
dim random

for i=0 to 10
randomTemp = randomNumber(122)
random = random & Chr(randomTemp)
next

generate = random
end function

function randomNumber(intHighestNumber)
randomize
randomNumber = int(rnd * intHighestNumber) + 1
end function

%>
Comment
Watch Question

## View Solution Only

Commented:
122-48 = 74

Find a random number between 1 and 74 then add 48 to it:

<%
FUNCTION randNumber
maxNumber = 74
Randomize
randNumber = INT((74 - 1 + 1) * Rnd) + Low
Response.Write (randNumber + 48)
END FUNCTION

randNumber()
%>
Commented:
Here is the whole code snippet .... you should be able to tweak this to fit your needs.  I have it printing out the random number and corresponding Char for that number to show what it is doing ....

<%
FUNCTION randNumber
maxNumber = 74
Randomize
randNumber = INT((74 - 1 + 1) * Rnd) + Low
randNumber = randNumber + 48
END FUNCTION

FUNCTION createString
DIM randomString
randomString = ""
FOR i=0 TO 10
randNumber()
Response.Write randNumber & "&nbsp;&nbsp;&nbsp;&nbsp;"
Response.Write Chr(randNumber) & "<br><br>"
NEXT
END FUNCTION

createString()
%>

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Great !

Thanks for the help Rocky.  Nice algorithm :)

completed code:

<%
function generate()

dim randomTemp
dim random

for i=0 to 10
randomTemp = randomNumber(97,122)
random = random & chr(randomTemp) & randomTemp & chr(10)
next

generate = random
end function

function randomNumber(min, max)

dim range
range = max - min

randomize
randomNumber = int(range*rnd) + min
end function

%>
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile