• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 887
  • Last Modified:

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

%>
0
rburello
Asked:
rburello
  • 2
1 Solution
 
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
 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now