We help IT Professionals succeed at work.

ASP random string

rburello
rburello asked
on
Medium Priority
902 Views
Last Modified: 2012-06-27
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

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()
%>
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.

Ask the Experts

Author

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

%>
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

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

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.