ASP random string

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

%>
LVL 2
rburelloAsked:
Who is Participating?
 
rockymageeCommented:
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()
%>
0
 
rockymageeCommented:
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()
%>
0
 
rburelloAuthor 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

%>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.